rm(list=ls())
library(foreign)
library(MASS)
library(graphics)
#Create a vector which contains the values of unique identifiers for each panel
##Make sure that each panel has a unique identifier. For example, if there are 10 panels for which you want to run separate regressions:
data <- read.csv('sample data.csv')
panel <- c(1:10)
n.panels <- 10
#Create vectors to store coefficients and standard errors for each independent variable that you want to graph
x1.coef <- c(rep(NA, n.panels))
x1.se <- c(rep(NA, n.panels))
x2.coef <- c(rep(NA, n.panels))
x2.se <- c(rep(NA, n.panels))
x3.coef <- c(rep(NA, n.panels))
x3.se <- c(rep(NA, n.panels))
#Run a loop over each panel, storing coefficients from each individual regression
for(i in panel){
subset.data <- as.data.frame(data[data$panelid==i,]) #where 'panelid' is the variable name of the unique identifier of the panel and 'data' is the assigned name of the dataset
fit <- lm(y ~ x1 + x2 + x3, data=subset.data) #run the appropriate model for your analysis
x1.coef[i] <- summary(fit)$coef[2,1]
x1.se[i] <- summary(fit)$coef[2,2]
x2.coef[i] <- summary(fit)$coef[3,1]
x2.se[i] <- summary(fit)$coef[3,2]
x3.coef[i] <- summary(fit)$coef[4,1]
x3.se[i] <- summary(fit)$coef[4,2]
}
#Create a vector of labels for the coefficients; for example, if the panels are 10 different countries:
coefs.and.ses <- as.data.frame(cbind(x1.coef,x1.se,x2.coef,x2.se,x3.coef,x3.se))
row.names(coefs.and.ses) <- c("USA","CAN","MEX","PAN","GUA","HON","BEL","NIC","SAL","COS")
attach(coefs.and.ses)
#Produce the V-graph for x1
png(file="myplot3.png") #tells R to generate a png file
plot(x=x1.coef,y=x1.se,pch=16,main="Coefficients and standard errors on x1",xlab="Coefficients",ylab="Standard Errors",xlim=c(-1,1),ylim=c(0,1))
text(x=x1.coef,y=x1.se,labels=row.names(coefs.and.ses), pos=2)
abline(a=0,b=1/1.96,untf=FALSE,lty=2)
abline(a=0,b=-1/1.96,untf=FALSE,lty=2)