我试图在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中的错误(公式=~变量+缓解+时间+:可变长度不同(“缓解”)
没有循环,我的分析就会很好(Lipid(x)只是脂质之一):
lme_cer <- lme(lipid(x) ~ Remission + Time + Age + BMI + SEX , random = ~1 | Lacal.Patient.ID, method = "REML", data = cer_data)
summary(lme_cer)有人能看到我的循环的问题吗?我不习惯编程或使用R,所以可能有一些愚蠢的错误。
发布于 2016-04-27 12:50:06
一个盲目的答案,假设你的因变量是按列而不是按行排列的(正如我想的那样)。
我的方法和你的方法的主要区别是,我循环了脂质的名称,而不是它们在数据集中的位置。这允许我(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。
发布于 2016-04-30 19:20:48
解决方案:我的循环现在正在处理以下简单代码:
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) }
谢谢大家的帮助!
发布于 2016-04-27 12:39:18
在不知道你的数据的情况下,概念上应该是这样的。
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)
}https://stackoverflow.com/questions/36889516
复制相似问题