I made 30 regression lines and I get different betas and r2, and I wan to make a graph with it.
So I have
model 1, that the data goes from 1870-1874
model 2, that data goes from 1875-1879
....Model 30, that data goes from 2015-2019
so what I want is to extract the coefficient ( just the beta, not the intercept) of each of the 30 regression line and plot it in a linear graph
I did the following script;
TFG2<-read.csv("TFG2.csv", stringsAsFactors = FALSE,
na.strings = c("NA", "#DIV/0!"))
Look at the year range for each country
lapply(unique(TFG2$country), function(x) range(TFG2$year[TFG2$country == x]))
All countries have data from 1870 to 2016
Group years:
y0 <- seq(1870, 2016, by = 5) ## start points of intervals
y1 <- seq(y0[2]-1, 2020, by = 5) ## end points of intervals
Average
aux <- lapply(unique(TFG2$country), function(c){
d <- TFG2[TFG2$country == c,]
res <- lapply(1:length(y0), function(i){
yy0 <- y0[i]
yy1 <- y1[i]
dd <- d[d$year >= yy0 & d$year <= yy1,]
data.frame(country = c,
period = paste0(yy0, "-", yy1),
t(apply(dd[,-c(1,2)], 2, mean, na.rm = TRUE)))
})
res <- as.data.frame(do.call("rbind", res))
})
TFG3 <- do.call("rbind", aux)
Linear model for each period
mod.period <- lapply(unique(TFG3$period), function(p){
d <- TFG3[TFG3$period == p,]
lm(iy ~ say, data = d)
})
Summary of the models
lapply(mod.period, summary)