2

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!

Nick
  • 833
  • 2
  • 8
  • 11
  • What is the expected output? – akrun Aug 05 '15 at 16:37
  • @akrun a list of the summaries by hour, not just the coefficients. The one you just posted was great except it didn't have the summaries because I want to see the significance of Par2 – Nick Aug 05 '15 at 16:40
  • You should use `summary(lm1)` and not `lm1$summary` – akrun Aug 05 '15 at 16:40
  • You can't fit the entire summary in a data.frame - what specific numbers do you want?? – Señor O Aug 05 '15 at 16:41
  • @SeñorO The Pr(>|t|) value in the coefficients table. I am trying to determine the significance of Par2 for each of the hours. – Nick Aug 05 '15 at 16:45

2 Answers2

6

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)
Rorschach
  • 31,301
  • 5
  • 78
  • 129
3

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
Señor O
  • 17,049
  • 2
  • 45
  • 47