我试图在for循环中使用来自nlme包的lme函数。我现在已经尝试了(差不多)一切,但没有运气。没有循环我的lme功能正常工作。我有681种不同的脂质需要分析,所以我需要循环。
奖金信息:
我的数据的简化版本如下所示:
>dput(head("ex.lme(loop)"))
structure(list(Lacal.Patient.ID = c(12L, 12L, 12L, 13L, 13L,
13L), Time = c(0L, 1L, 3L, 0L, 1L, 3L), Remission = c(0L, 0L, 1L, 0L, 0L, 1L), Age = c(46L, 43L, 36L, 47L, 34L, 45L), SEX = structure(c(1L, 1L, 1L, 2L, 2L, 2L), .Label = c("f", "m"), class = "factor"), BMI = c(25L, 26L, 23L, 27L, 26L, 27L), Sph = c(0.412, 1.713,
1.48, 0.735, 1.025, 1.275), S1P = c(2.412, 3.713, 3.48, 2.735,
3.025, 3.275), Cer..C16. = c(1.4472, 2.2278, 2.088, 1.641,
1.815, 1.965)), .Names = c("Lacal.Patient.ID", "Time", "Remission", "Age",
"SEX", "BMI", "Sph", "S1P", "Cer..C16."), row.names = c(NA, 6L
), class = "data.frame")
以下是我做的事情:
library(nlme)
attach(cer_data)
Remission <- factor(Remission)
Time <- factor(Time)
SEX <- factor(SEX)
lipid <-as.matrix(cer_data[,c(7:9)]) # my lipids a at row 7-9in my data
beg <- 1
end <- nrow(lipid)
dim(lipid)
for (i in beg:end) {
print(paste("Running entity: ", colnames(lipid)[i], " which is ",i, " out of", end))
variable <- as.numeric(lipid[i])
lme_cer <- lme(variable ~ Remission + Time + Age + BMI + SEX, random = ~1|Lacal.Patient.ID, method = "REML", data = cer_data)
}
错误:model.frame.default错误(公式=〜变量+缓解+时间+:变量长度不同(找到'缓解')
lme_cer <- lme(lipid(x) ~ Remission + Time + Age + BMI + SEX , random = ~1 | Lacal.Patient.ID, method = "REML", data = cer_data)
summary(lme_cer)
任何人都可以看到我的循环问题吗?我不习惯编程或使用R,因此可能存在一些愚蠢的错误。
答案 0 :(得分:1)
一个盲目的答案,假设您的因变量按列而不是按行排列(我认为它们是这样)。
我的方法和你的方法之间的主要区别在于我循环了脂质的名称而不是它们在数据集中的位置。这允许我(a)以较少出错的方式构造临时数据集,以及(b)为模型的固定效果部分构造临时公式。
然后将lme
函数应用于具有临时公式的临时数据集,并将结果保存在列表中以便于访问。
# names of lipids
lipid.names <- colnames(cer_data)[1:881]
no.lipids <- length(lipid.names)
# create a named list to hold the fitted models
fitlist <- as.list(1:no.lipids)
names(fitlist) <- lipid.names
# loop over lipid names
for(i in lipid.names){
# print status
print(paste("Running entity:", i, "which is", which(lipid.names==i), "out of", no.lipids))
# create temporary data matrix and model formula
tmp <- cer_data[, c(i,"Remission","Time","Age","BMI","SEX","Local.Patient.ID")]
fml <- as.formula( paste( i, "~", paste(c("Remission","Time","Age","BMI","SEX"), collapse="+") ) )
# assign fit to list by name
fitlist[[i]] <- lme(fml, random=~1|Lacal.Patient.ID, method="REML", data=tmp)
}
在我看来,最简单的方法是使用完全包含循环迭代所需内容的临时对象。
请注意,我无法检查此解决方案是否存在错误,因为您尚未提供可重现的示例:Here's how。
答案 1 :(得分:1)
lipid <-as.data.frame(cer_data[,c(7:9)])
dim(lipid)
for (i in 1:length(lipid)) {
variable <- lipid[,i]
lme_cer <- lme(variable ~ factor(Remission) + Time + Age + BMI + SEX, random = ~1 | Lacal.Patient.ID, method = "REML", data = cer_data)
print(summary(lme_cer)$tTable)
}
谢谢大家的帮助!
答案 2 :(得分:0)
在不知道您的数据的情况下,从概念上讲它应该是那样的
df <- data.frame(lipid = rep(c(LETTERS[1:4]), each = 4), x1 = c(rnorm(16, 10, 1)), x2 = c(rnorm(16, 20, 5) ))
df
for (i in levels(df$lipid)){
print(paste("MODEL", i, sep = ""))
df1 = subset(df, lipid == i)
model <- lm(x1~x2, data = df1 )
print(summary(model)$coef)
}