Skip to content

Instantly share code, notes, and snippets.

@Protonk
Created November 23, 2010 19:16
Show Gist options
  • Save Protonk/712330 to your computer and use it in GitHub Desktop.
Save Protonk/712330 to your computer and use it in GitHub Desktop.
WDI Beta over Time
#Build a dataframe for the value of Beta Across the years
i<-0
beta.years<-matrix(0,2,30);
#This is a control structure which calculates a linear model for each year in the dataset and fills
#in the point estimate for the slope and the stardard error of that estimate into a matrix which
#we turn into a data frame.
for(i in sort(unique(GDP.giant$year))) {#
beta.years[1,i-min(unique(GDP.giant$year))+1]<-coef(summary(lm(lambda~log(GDP.per),data=subset(GDP.giant,year==i))))[2,1]#
beta.years[2,i-min(unique(GDP.giant$year))+1]<-coef(summary(lm(lambda~log(GDP.per),data=subset(GDP.giant,year==i))))[2,2]
}
GDP.beta<-data.frame(t(beta.years),row.names=c(sort(unique(GDP.giant$year))))
names(GDP.beta)<-c("Beta","S.E.");
#construct confidence intervals for beta from computed standard errors and the (probably wrong) assumption of normally distributed errors.
GDP.beta$lower<-with(GDP.beta,Beta-(qnorm(0.975)*S.E.));
GDP.beta$upper<-with(GDP.beta,Beta+(qnorm(0.975)*S.E.));
#Plot confidence intervals
betaint.plot<-qplot(as.numeric(dimnames(GDP.beta)[[1]]),lower,data=GDP.beta,geom="line",main="Beta and 95% Confidence Intervals")+geom_line(mapping=aes(as.numeric(dimnames(GDP.beta)[[1]]),Beta,data=GDP.beta),size=2,colour="blue")+geom_line(mapping=aes(as.numeric(dimnames(GDP.beta)[[1]]),upper,data=GDP.beta))+scale_y_continuous("Beta")+scale_x_continuous('');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment