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.