Question: Example data: a
Example data:
a <- rnorm(1:100) b <- c(replicate(25, "2008"), replicate(25, "2009"), replicate(25, "2010"), replicate(25, "2011")) c <- data.frame(a, b)
My code:
sp.b <- split(c,list(c$b)) sp.b
regFY <- function(df) { sp.q4 <- split(df,list(df$b)) for(i in sp.q4) { summary(lm(b ~ a, sp.q4[[i]])) } }
Question:
Create a function regFY() that takes a data frame as an argument and uses lm() to fit a least squares regression of a on b. Also create a function estSlope() that extracts the coefficient of year from the output of lm().
I tried to split the data on year so we have 4 data frames and then fit a linear regression on each datafarme. Unfortunately it doesnt work out.
Can you provide my with a function for regFY() and estSlope() please. Thank you.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
