Sie sind auf Seite 1von 4

#Loading the mice package

library(mice)

#Loading the following package for looking at the missing values

library(VIM)

library(lattice)

data(nhanes)

# First look at the data

str(nhanes)

#Convert Age to factor

nhanes$age=as.factor(nhanes$age)

#understand the missing value pattern

md.pattern(nhanes)

#plot the missing values

nhanes_miss = aggr(nhanes, col=mdc(1:2), numbers=TRUE, sortVars=TRUE,


labels=names(nhanes), cex.axis=.7, gap=3, ylab=c("Proportion of missingness","Missingness
Pattern"))

#Drawing margin plot

marginplot(nhanes[, c("chl", "bmi")], col = mdc(1:2), cex.numbers = 1.2, pch = 19)

#Imputing missing values using mice

mice_imputes = mice(nhanes, m=5, maxit = 40)


#What methods were used for imputing

mice_imputes$method

mice_imputes$imp$chl

#Imputed dataset

Imputed_data=complete(mice_imputes,5)

#Plotting and comparing values with xyplot()

xyplot(mice_imputes, bmi ~ chl | .imp, pch = 20, cex = 1.4)

#make a density plot

densityplot(mice_imputes)

#fit a linear model on all datasets together

lm_5_model=with(mice_imputes,lm(chl~age+bmi+hyp))

#Use the pool() function to combine the results of all the models

combo_5_model=pool(lm_5_model)#Loading the mice package

library(mice)

#Loading the following package for looking at the missing values

library(VIM)

library(lattice)

data(nhanes)
# First look at the data

str(nhanes)

#Convert Age to factor

nhanes$age=as.factor(nhanes$age)

#understand the missing value pattern

md.pattern(nhanes)

#plot the missing values

nhanes_miss = aggr(nhanes, col=mdc(1:2), numbers=TRUE, sortVars=TRUE,


labels=names(nhanes), cex.axis=.7, gap=3, ylab=c("Proportion of missingness","Missingness
Pattern"))

#Drawing margin plot

marginplot(nhanes[, c("chl", "bmi")], col = mdc(1:2), cex.numbers = 1.2, pch = 19)

#Imputing missing values using mice

mice_imputes = mice(nhanes, m=5, maxit = 40)

#What methods were used for imputing

mice_imputes$method

mice_imputes$imp$chl
#Imputed dataset

Imputed_data=complete(mice_imputes,5)

#Plotting and comparing values with xyplot()

xyplot(mice_imputes, bmi ~ chl | .imp, pch = 20, cex = 1.4)

#make a density plot

densityplot(mice_imputes)

#fit a linear model on all datasets together

lm_5_model=with(mice_imputes,lm(chl~age+bmi+hyp))

#Use the pool() function to combine the results of all the models

combo_5_model=pool(lm_5_model)

Das könnte Ihnen auch gefallen