简体   繁体   English

密度错误。默认值…至少需要2点才能自动选择带宽

[英]Error in density.default…need at least 2 points to select a bandwidth automatically

I am using the mice package to obtain completed data. 我正在使用mice包来获取完整的数据。 I believe the problem is that I am not doing imputations for all the data, and so some of them have NAs. 我认为问题在于我没有对所有数据进行估算,因此其中一些具有NA。 (some variables with missing data are used simply to predict missingness in others so I don't want to impute those. (某些数据缺失的变量仅用于预测其他数据的缺失,因此,我不想对这些变量进行估算。

I can duplicate the problem with this code: 我可以用以下代码复制问题:

require(mice)
impute <- mice(
    nhanes, 
    imputationMethod = c(
        "",        # age
        "pmm",     # bmi
        "pmm",  # hyp
        ""         # chl
    ),
    seed = 101)
x11()
densityplot(impute)

Error in density.default(x = c(NA_real_, NA_real_, NA_real_, NA_real_,  : 
  need at least 2 points to select a bandwidth automatically

How can I get the densityplots ? 我如何获得密度图? If I replace the "" with "pmm" for chl or just run impute <- mice(nhanes) then it will work with this example, to produce this: 如果我将chl""替换为"" "pmm"或仅运行impute <- mice(nhanes)则它将与本示例一起使用,以产生以下内容: 在此处输入图片说明

but I can't do that with my own data, so I'm looking for another way....just to get the density plots for bmi and hyp , after running mice with my code above which does not impute values for chl 但是我不能用自己的数据做到这一点,所以我正在寻找另一种方法....只是在用我的代码运行mice之后才获得bmihyp的密度图,这并没有推论chl

Edit: I know I can use the methods in the answer to my earlier question using ggplot , but in this case I really need to work with densityplot 编辑:我知道我可以使用ggplot回答先前问题的方法,但是在这种情况下,我确实需要使用densityplot

You will still have a large number of missing data in the 'chl'-column. 在“ chl”列中,您仍然会丢失大量数据。 You could use the mice function complete to pull together the original non-missing and the imputed values. 您可以使用complete的mouses功能将原始非缺失值和估算值汇总在一起。 But the densitplot.mids function will do it if you reverse the roles of data and formula as (eventually) described in the Details of hte help page. 但是,如果您按照(最终)在hte帮助页面的详细信息中所述反转数据和公式的角色,则densitplot.mids函数将执行此操作。

densityplot( x=impute , data= ~ bmi+hyp)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM