在R中对多个文件运行线性回归

时间:2012-03-16 20:03:36

标签: r

我有一个包含来自多个仪器运行的20个文本文件的文件夹。数据部分都具有相同的格式

22/05/11;   16:03:28;       0.000;   6.079;   31.41;   84881;   25.60; E0;
22/05/11;   16:03:29;       0.017;   6.063;   31.44;   84868;   25.60; E0;
22/05/11;   16:03:30;       0.034;   6.079;   31.41;   84868;   25.60; E0;
22/05/11;   16:03:31;       0.051;   6.079;   31.41;   84868;   25.60; E0;
22/05/11;   16:03:32;       0.068;   6.068;   31.43;   84868;   25.60; E0;
22/05/11;   16:03:33;       0.085;   6.068;   31.43;   84881;   25.60; E0;
22/05/11;   16:03:34;       0.102;   6.079;   31.41;   84874;   25.60; E0;

我想要做的是读取我文件夹中的每个文件,运行线性回归,并拉出斜率和R2值。

截至目前,这是我为单个文件执行此操作的代码。

O2=read.table("Coral 1_Dark.txt",skip=58, sep=";",header=FALSE)
names(O2)<-c("Date","Time","Log_Time","O2_mgL","Phase","Amp","Temp C","Error Message")
O2$id<-seq_len(nrow(O2)) #creates unique ID for each measurement (use for regression)
attach(O2)
fit=lm(O2_mgL~id)
summary(fit)

运行此代码后,我手动输入斜率和R2数据。

现在我可以使用

创建一个包含我感兴趣的所有文件的变量
F=list.files()

这给了我所有20个文件

[1] "Coral 1_Dark.txt"   "Coral 1_Light.txt"  "Coral 10_Dark.txt"  "Coral 10_Light.txt" "Coral   2_Dark.txt"  
[6] "Coral 2_Light.txt"  "Coral 3_Dark.txt"   "Coral 3_Light.txt"  "Coral 4_Dark.txt"   "Coral  4_Light.txt" 
[11] "Coral 5_Dark.txt"   "Coral 5_Light.txt"  "Coral 6_Dark.txt"   "Coral 6_Light.txt"  "Coral 7_Dark.txt"  
[16] "Coral 7_Light.txt"  "Coral 8_Dark.txt"   "Coral 8_Light.txt"  "Coral 9_Dark.txt"   "Coral 9_Light.txt" 

我想最终得到的就是这样的所有20个文件

Coral            Slope        R2
Coral 1_Dark      0.23         98.3
Coral 2_Dark      0.33         99.3

ECT

有什么建议吗?我从来没有使用任何应用函数或任何类型的循环 - 但我认为这将改变.....

3 个答案:

答案 0 :(得分:3)

这样的东西?

wd <- "C:/Data"
files    <- dir(wd)
varnames <- c("Date", "Time", "Log_Time", "O2_mgL", "Phase", "Amp", "Temp C",
            "Error Message")
results  <- data.frame()

for (i in 1:length(files)) {
  fname <- paste(wd, files[i], sep="/")
  data <- read.table(fname, sep=";", skip=58)
  colnames(data) <- varnames
  data$id <- 1:nrow(data)
  fit <- summary(lm(O2_mgL~id, data=data))
  results[i,1] <- fit$coefficients[2]
  results[i,2] <- fit$r.squared
}

rownames(results) <- sub(".txt", "", files)
colnames(results) <- c("Slope", "R2")

print(results)

答案 1 :(得分:1)

此代码可能有机会。使用attach并不是一个好主意,尤其是在创建函数时。

Coral_1_Dark.txt <- "22/05/11;   16:03:28;       0.000;   6.079;   31.41;   84881;   25.60; E0;
+ 22/05/11;   16:03:29;       0.017;   6.063;   31.44;   84868;   25.60; E0;
+ 22/05/11;   16:03:30;       0.034;   6.079;   31.41;   84868;   25.60; E0;
+ 22/05/11;   16:03:31;       0.051;   6.079;   31.41;   84868;   25.60; E0;
+ 22/05/11;   16:03:32;       0.068;   6.068;   31.43;   84868;   25.60; E0;
+ 22/05/11;   16:03:33;       0.085;   6.068;   31.43;   84881;   25.60; E0;
+ 22/05/11;   16:03:34;       0.102;   6.079;   31.41;   84874;   25.60; E0;

list_of_summaries <- sapply( 'Coral_1_Dark.txt', function(nam) { 
    O2 <- read.table(file=textConnection(get(nam)), sep=";",header=FALSE)
    names(O2) <- c("Date","Time","Log_Time","O2_mgL","Phase", 
                               "Amp","Temp C","Error Message", "junk")
    O2$id <- seq_len( nrow(O2) )
    fit=lm(O2_mgL~id , data=O2)
    summ <- summary(fit) 
    return( c(slope= coef(fit)["id"], R2= summ[["r.squared"]] ) )  })

as.data.frame( list_of_summaries )

答案 2 :(得分:0)

是的,您即将获得应用功能的介绍。基本上对文件夹执行dir()调用,然后使用单个文件参数将您在事件中完成的所有内容包装起来。并返回您感兴趣的结果。然后使用函数作为第二个参数调用文件列表上的lapply