r - Looping linear models for multiple files in directory -
i have folder 26 .csv files in it. each file has 2 columns headers do2
, time_min
, have @ least 300+ rows.
i want make scatterplot x=time_min
, y=do2
, make linear model of each, take coefficient
, r^2
each of 26 models , put in table.
this i've written far code goes. know can copy , paste know there has smarter way.
setwd("~/documents/masters/data/r/35789/35789_ucrit") #the file want coefficients , r^2 go ue_slope <- read.csv("~/documents/masters/data/r/35789/35789_ue_slope.csv") temp = list.files(pattern="*.csv") (i in 1:length(temp))(assign(temp[i], read.csv(temp[i]))) #seal# names files directory, 1-26 plot(do2 ~ time_min, data = seal1) model1 <- lm(do2 ~ time_min, data = seal1.csv) ue_slope <- rbind(ue_slope, data.frame("slope"=coef(model1)[[2]], "r.2"=summary(model1)$r.squared))
we first define function, reads "csv" file, fits linear model , obtains summary statistics.
f <- function (file) { ## read file dat <- read.csv(file) ## fit model fit <- lm(do2 ~ time_min, data = dat) slope <- coef(fit)[2] ## make plot?? plot(do2 ~ time_min, data = dat, main = file) ## use file names title abline(fit) ## overlay fitted regression line ## note, not using `summary.lm` expensive ## r-squared can computed rss <- crossprod(fit$residuals)[1] tss <- crossprod(dat$do2 - mean(dat$do2))[1] r2 <- 1 - rss / tss ## return vector c("slope" = slope, "r.2" = r2) }
now, loop through files, applying f
:
temp <- list.files(pattern = "*.csv") pdf("whatever.pdf") result <- t(sapply(temp, f)) dev.off()
sapply
cbind
ends flat matrix; use t()
make tall matrix. pdf()
, dev,off()
opens / closes pdf file , plots made on file. looks necessary have 26 figures, not easy display them in panel fashion on screen. using pdf file, can have 1 plot per page. pdf file in current working directory.
Comments
Post a Comment