简体   繁体   中英

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

I am using the mice package to obtain completed data. I believe the problem is that I am not doing imputations for all the data, and so some of them have NAs. (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: 在此处输入图片说明

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

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

You will still have a large number of missing data in the 'chl'-column. You could use the mice function complete to pull together the original non-missing and the imputed values. 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.

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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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