循环r中具有不同列数的数据帧

时间:2013-03-27 23:59:14

标签: r

也许是微不足道的但我正试图解决这个问题:

我需要数据框,一个有25个,另一个有9列。现在,我需要做的是拟合多项式方程,其中我的因变量在25列的数据框中,而我的自变量在9列的数据框中。 目前,我将这些列组合在一起并创建了一个名为“my.data”的数据框,因此我在当时使用一个自变量循环遍历因变量。但是,我想自动在循环中执行25 * 9次的功能。有没有办法做到这一点?

setwd("C:\\......")

 my.data <- read.table("MyData.txt", header = TRUE, sep = "\t")


for(i in seq_along(my.data))
 {

    fit1b <- lm(my.data[ ,i] ~ my.data$V1)
    fit2b <- lm(my.data[ ,i] ~ poly(my.data$V1, 2, raw=TRUE))
    fit3b <- lm(my.data[ ,i] ~ poly(my.data$V1, 3, raw=TRUE))
    poly1 <-capture.output(summary(fit1b))
    poly2 <-capture.output(summary(fit2b))
    poly3 <-capture.output(summary(fit3b))


con = file(description = "MyResults.txt", open="a")
write.table(poly1, file= con, append = TRUE, quote=F, col.names=FALSE, row.names= F)
write.table(poly2, file= con, append = TRUE, quote=F, col.names=FALSE, row.names= F)
write.table(poly3, file= con, append = TRUE, quote=F, col.names=FALSE, row.names= F)
close(con)
 }

2 个答案:

答案 0 :(得分:1)

这是使用mapplyexpand.grid

的绝佳机会

例如。

# some dummy data
xx <- data.frame(replicate(5, runif(50)))
yy <- setNames(data.frame(replicate(3, runif(50))), paste0('Y',1:3))
# all combinations
cs <- expand.grid(list(pred = names(xx), resp = names(yy)), stringsAsFactors= FALSE)

# a function to do the fitting
fitting <- function(pred, resp, dd){
  # fit linear model
  ff <- reformulate(pred, resp)
  lmf <- lm(ff, data =dd)
  # create a formula for poly(,2)
  ff.poly2 <- update(ff, .~poly(.,2, raw=TRUE))
  # and poly(,3)
  ff.poly3 <- update(ff, .~poly(.,3, raw=TRUE))
  # fit these models
  lmp2 <- lm(ff.poly2, data = dd)
  lmp3 <- lm(ff.poly3, data = dd)
  # return a list with these three models
  list(linear = lmf, poly2 = lmp2, poly3 = lmp3)
}

biglist <- mapply('fitting', pred = as.list(cs[['pred']]), 
        resp = as.list(cs[['resp']]),
       MoreArgs = list(dd = cbind(xx,yy)), SIMPLIFY = FALSE)

# give this list meaningful names

names(biglist) <- do.call(paste, c(cs, sep = ':'))

然后,您可以使用一些嵌套的lapply语句

来提取/汇总内容

例如所有线性模型的摘要

lapply(lapply(biglist, `[[`,'linear'), summary)

的二次模型

lapply(lapply(biglist, `[[`,'poly2'), summary)

如果您想在单个文件中提取print(summary(lm))中的信息,例如

capture.output(lapply(biglist, function(x) lapply(x, summary)), file = 'results.txt')

将创建一个名为results.txt的文件,其中打印了所有结果。

答案 1 :(得分:0)

我想做一件事,输出摘要而不是列表,但我不确定是否可以使用你的写作功能。有没有办法获得这个?

呼叫: lm(公式= My-Y-Lable~My-X-Label)

残差:      Min 1Q Median 3Q Max -0.35445 -0.17420 -0.10931 0.06975 0.60246

系数:              估计标准。误差t值Pr(&gt; | t |)
(拦截)0.7560212 0.0720984 10.49 1.24e-14 *

My-X-Label 0.0072100 0.0006597 10.93 2.68e-15 *

Signif。代码:0' '0.001' '0.01''0.05'。'0.1''1

残余标准误差:54自由度为0.2812 多个R平方:0.6887,调整后的R平方:0.6829 F统计量:119.5 on 1和54 DF,p值:2.676e-15