我正在尝试运行这个残差函数,volumes_dt_screening 包含我的数据:
vol_resid_func <- function(y) resid(lm(y ~ volumes_dt_screening$EstimatedTotalIntraCranialVol + volumes_dt_screening$eWBV + volumes_dt_screening$Age + volumes_dt_screening$Sex + volumes_dt_screening$Education + volumes_dt_screening$Scanner_Site, data = volumes_dt_screening))
但是,当将函数应用于我的数据框中的因变量时:
vol_resid <- as.data.frame(lapply(volumes_dt_screening[2:17], vol_resid_func))
我收到以下错误:“model.frame.default(formula = y ~ volumes_dt_screening$EstimatedTotalIntraCranialVol + 中的错误: 变量“y”的无效类型(列表)“
即使我修改了数据框,所以没有列表
volumes_dt_screening <- as.data.frame(volumes_dt_screening)
存在同样的错误。
所以我尝试修改残差函数:
vol_resid_func <- function(y) {
y <- unlist(y)
resid(lm(y ~ EstimatedTotalIntraCranialVol + eWBV + Age + Sex + Education + Scanner_Site, data = volumes_dt_screening))
}
在应用它后,我得到了一个不同的错误:“model.frame.default(formula = y ~ EstimatedTotalIntraCranialVol + : 可变长度不同(针对“EstimatedTotalIntraCranialVol”找到)“
我试过包括一种删除 NA 值的方法(没有,但可以肯定)
vol_resid_func <- function(y) {
y <- na.omit(unlist(y))
resid(lm(y ~ EstimatedTotalIntraCranialVol + eWBV + Age + Sex + Education + Scanner_Site, data = volumes_dt_screening))
}
但是出现同样的变长错误。
如果我在函数中包含 print(length(y)) 以查看长度,我不会在控制台中获得输出。
我不确定从这里去哪里。任何指导将不胜感激!
如前所述,我尝试取消列出 y 变量,省略 NA 值,以及打印 (y) 以查看预测变量值为何告诉我这个。