我想从线性混合模型循环中提取残差。
results <- matrix(NA, nrow(meth),4, dimnames=list(rownames(meth), c("nobs", "coef", "coef.se", "Pval")))
循环创建一个矩阵,其中包含变量X的结果,但现在我想从该循环中提取残差,这将创建与meth(相同维度)相同的矩阵,但随后与残差一起。我试过了:
#make matrix
res<- matrix(NA, nrow(meth),ncol(meth), dimnames=list(rownames(meth), colnames(meth)) )
#run loop
for (i in 1:(nrow(meth))) {
y <- (meth[i,])
model <- try(lmer(as.formula(f), data=(sample_trans))
, silent=TRUE)
if (!inherits(model, "try-error")) {
res[i,] <- resid(model)
}
}
但是我收到了错误:
Error in res[i, ] <- resid(model) : number of items to replace is not a multiple of replacement length
数据中有NA,但不应该影响它,因为我用过try。循环本身有什么问题吗?