在R中使用鼠标将数据帧转换为mids



我用鼠标输入数据,将数据保存为csv,然后在SPSS中运行因子分析,生成一些因子。我现在想在R中加载csv,并对数据进行估算线性回归。然而,当我试图将数据帧转换为midd时,我收到一条错误消息,上面写着:

library(mice)
# assign mtcars to a new dataframe
df <- mtcars
# loop 10 times
for (x in 1:10){

# create a fake imp number
a <- rep(x, 1, nrow(df))

# bind the fake imp number to the df
df2 <- cbind(df, a)

# crate a 10 folded version of mtcars with also the fake imp number
if (x ==1){
new_df <- df2
} else{
new_df <- rbind(new_df, df2)
}
}
# change the column name of the fake imp to ".imp"
names(new_df)[names(new_df) == 'a'] <- '.imp'
# convert df to mids
df_imp <- as.mids(new_df, .imp = .imp)
> Error in as.mids(df) : Original data not found. Use `complete(...,
> action = 'long', include = TRUE)` to save original data.

你能帮我纠正这个错误吗?

来自as.mids()文档。

此函数将以长格式存储的估算数据转换为mids类的对象原始的不完整数据集需要可用,以便我们知道丢失的数据在哪里。该函数可用于将应用于mids对象中估算数据的运算转换回。它还可以用于将来自其他软件的多次估算数据集存储为鼠标使用的格式。

不完整数据以长格式存储为插补0。因此,在0而不是1启动过程可以解决此问题。(此外,在as.mids()调用中,您需要在.imp = '.imp'周围加引号。或者,删除它并依赖默认值。或者,只提供"a"作为插补变量。(

library(mice)
df <- mtcars
for (x in 0:10){
a <- rep(x, 1, nrow(df))
df2 <- cbind(df, a)
if (x == 0){
new_df <- df2
} else{
new_df <- rbind(new_df, df2)
}
}
names(new_df)[names(new_df) == 'a'] <- '.imp'
df_imp <- as.mids(new_df)

最新更新