在R中为lme()循环 [英] Making a loop for lme() in r
问题描述
我正在尝试在for循环中使用nlme包中的lme函数.我已经尝试了几乎所有的一切,但是没有任何运气.没有循环,我的lme函数可以正常工作.我有681种不同的脂质要分析,因此我需要进行定量分析.
I am trying to use lme function from nlme package inside a for loop. I have tried (almost) everything now, but without any luck. Without the loop my lme function are working fine. I have 681 different lipids to analyse, so i need the loop.
奖金信息:
- 我使用过str(),并且我的数据在循环之前具有相同的长度
我的数据的简化版本如下:
A simplified version of my data look like this:
>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")
>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)
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中出现错误(公式=〜变量+缓解+时间+:变量长度不同(为缓解"找到)
Error : Error in model.frame.default(formula = ~variable + Remission + Time + : variable lengths differ (found for 'Remission’)
没有循环,我的分析工作就很好了(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,因此可能存在一些愚蠢的错误.
Can anyone see the problem with my loop? I am not used to programming or using R, so there is probably some stupid mistakes.
推荐答案
一个盲目的答案,假设您的因变量是按列而不是按行组织的(按照我的想法).
A blind answer, assuming that your dependent variables are organized in columns and not in rows (as I think they are).
我的方法和您的方法之间的主要区别是,我遍历脂质的名称,而不是它们在数据集中的位置.这使我(a)以不太容易出错的方式构造临时数据集,并且(b)为模型的固定效应部分构造临时公式.
The main difference between my approach and your approach is that I loop over the names of the lipids rather than their position in the data set. This allows me (a) to construct a temporary data set in a less error-prone way, and (b) to construct a temporary formula for the fixed-effects part of your model.
然后将lme
函数应用于具有临时公式的临时数据集,并将结果保存在列表中,以方便访问.
The lme
function is then applied to the temporary data set with the temporary formula, and the result is saved in a list for easier access.
# 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)
}
我认为,最简单的方法是使用完全包含该循环迭代所需内容的临时对象.
In my opinion it's easiest to work with temporary objects that exactly contain what is needed at that iteration of the loop.
请注意,由于您尚未提供可复制的示例,因此我无法检查此解决方案的错误:
Note that I cannot check this solution for errors because you haven't supplied a reproducible example: Here's how.
这篇关于在R中为lme()循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!