2

I’ve been trying (for several days now) to “automate” several linear regressions that use the same x axis data (”Methane”) but the y axis varies (days 1, 10, 16, etc.). From each regression, I want to extract the Intercept and Slope and write them under each respective column (days 1 to 72). I also still don’t know which approach would be more suitable, if for loop or s/lapply.

Example (data frame called "raw_standards"):

Methane 1        10       16       62       72 
224.62  1490700  1423400  2475400  2063300  1819650 
449.23  3297100  2878950  4980300  4078800  3701750 
842.32  4181900  5292200  10718500 8247400  7566600 
2246.18 9211500  12535000 25439000 19867500 16443000 
4492.36 29228000 27567000 49345000 39328000 30743000 

My current for loop is as follows:

for (i in raw_standards[,2:6]) {
  y = raw_standards [,3:20]
  lm(y ~ raw_standards$'uM Methane', raw_standards)
}

I also tried with lapply:

lapply (raw_standards [ , 2:6],
  lm(raw_standards [ , 2:6] ~ raw_standards$'uM Methane',raw_standards))

Any help to understand how properly write the right code is much appreciated.

Karolis Koncevičius
  • 9,417
  • 9
  • 56
  • 89

1 Answers1

2

I like to do these kind of things using pipes. A brilliant combo of three packages from hadley will do the trick.

df <- data.frame(Methane = rnorm(30),Day1 = rnorm(30),Day2 = rnorm(30),Day3 = rnorm(30))

library(tidyr)
library(dplyr)
library(purrr)

df %>% gather(Variable,value,-Methane) %>% split(.$Variable) %>% 
      map(~ lm(value ~ Methane, data = .)) %>%
      map("coefficients") 

First step: gather function converts the data into long format.

Second step: Split functions the dataframe into a list of dataframes.

Third step: map builds linear model(same as lapply) on all elements of the list

For further clarification, see package vignettes of these packages which were very well written. The output of the above code is :

$Day1
(Intercept)     Methane 
 0.17664660 -0.07090221 

$Day2
(Intercept)     Methane 
 0.03615358  0.24230124 

$Day3
(Intercept)     Methane 
  0.1662604  -0.2836147 
Koundy
  • 5,265
  • 3
  • 24
  • 37