Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在r中为lme()生成循环_R_Loops_Nlme - Fatal编程技术网

在r中为lme()生成循环

在r中为lme()生成循环,r,loops,nlme,R,Loops,Nlme,我试图在for循环中使用nlme包中的lme函数。我现在几乎什么都试过了,但运气不好。没有环路,我的lme功能工作正常。我有681种不同的脂质要分析,所以我需要循环 奖金信息: 我使用了str,我的数据在循环之前具有相同的长度 我的数据的简化版本如下所示: >dputheadex.lmeloop structurelistLacal.Patient.ID=c12L,12L,12L,13L,13L, 13L,时间=c0L,1L,3L,0L,1L,3L,缓解=c0L,0L,1L,0L,0L,1L,年

我试图在for循环中使用nlme包中的lme函数。我现在几乎什么都试过了,但运气不好。没有环路,我的lme功能工作正常。我有681种不同的脂质要分析,所以我需要循环

奖金信息:

我使用了str,我的数据在循环之前具有相同的长度 我的数据的简化版本如下所示:

>dputheadex.lmeloop structurelistLacal.Patient.ID=c12L,12L,12L,13L,13L, 13L,时间=c0L,1L,3L,0L,1L,3L,缓解=c0L,0L,1L,0L,0L,1L,年龄=c46L,43L,36L,47L,34L,45L,性别=structurec1L,1L,1L,2L,2L,2L,2L,.Label=cf,m,class=factor,BMI=c25L,26L,23L,27L,26L,27L,Sph=c0.412,1.713, 1.48,0.735,1.025,1.275,S1P=c2.412,3.713,3.48,2.735, 3.025、3.275、Cer..C16=c1.4472、2.2278、2.088、1.641、, 1.815,1.965,.Names=cLacal.Patient.ID,时间,缓解,年龄, 性别、BMI、Sph、S1P、Cer..C16、行名=cNA、6L ,class=data.frame

以下是我所做的:

图书馆 附件数据
如果不知道你的数据,概念上应该是这样的

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)
}

一个盲目的回答,假设你的因变量是按列组织的,而不是按我认为的行组织的

我的方法和你的方法之间的主要区别是我循环了脂质的名称,而不是它们在数据集中的位置。这允许我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)

}
在我看来,使用临时对象是最容易的,这些临时对象正好包含循环迭代所需的内容

请注意,我无法检查此解决方案是否存在错误,因为您没有提供可复制的示例:。

解决方案:我的循环现在正在使用以下简单代码:
现在你为每一行做lme,这是错误的。查看end并输入用于重现问题的数据。您应该重新评估数据集中的行和列中的内容。四个观察:首先,在创建脂质数据时,从完整数据集中选择许多列。第二,出于某种原因,你在第1-881行写下我的脂质,这肯定是不正确的。第三,创建从1到nRowlipid的循环runnin,即使变量在列中而不是行中。最后,在循环中,您选择lipid[i],它既不选择行也不选择列,只选择该矩阵的单个元素。如果您有解决方案,请将其作为答案发布,而不是编辑您的问题??nlme有一些内置函数,用于拟合多个模型,可以代替循环。看到了吗
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)
}
# 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)

}