I have a dataframe as follows:
df
Date Hour ID Par1 Par2 Par3
08-01-15 0 A 2 3 4
08-01-15 0 B 4 5 6
08-01-15 1 N 2 9 10
08-01-15 1 A 3 7 23
08-01-15 1 B 4 7 22
08-02-15 0 E 2 4 12
08-02-15 0 A 3 7 9
So I want to split this dataframe by Hour as follows:
splitdata<-split(df<-split(df, df$Hour)
After splitting it, I want to apply a linear model to the split dataset.
result <- lapply(splitdata, function(df){
lm1 <-lm(Par1~Par2,data=df)
summary <- (lm1$summary)
data.frame(as.list(summary))
})
result
My results do not show anything. Though if I changed summary to:
summary <- (lm1$coef)
data.frame(as.list(summary))
Then it will produce a result.
So the main question is, how do I get a list of the summaries of each of the linear models by Hour and not just the coefficients?
Thanks!
Try using lmList from nlme
library(nlme)
fits <- lmList(Par1 ~ Par2 | Hour, data=df)
This splits the data by hour and fits linear models for you. Then, you can just do summary(fits). Or, you can look at each summary individually with
lapply(fits, summary)
If you're looking for a table form (which I usually prefer):
> dt = data.table(df)
> dt[,{S = summary(lm(Par1 ~ Par2))$coefficients; list(Coef = S[2,1], Intercept = S[1,1], Sig = S[2,4])}, by = Hour]
Hour Coef Intercept Sig
1: 0 0.314286 1.25714 0.439388
2: 1 -0.750000 8.75000 0.333333
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With