A Monte Carlo Simulation Program For Linear Regression Parameters Written In R


# a monte carlo simulation for regression parameters by baris altayligil
# deparment of economics/istanbul university 2010
x<-runif(1000,0,50) #random numbers from uniform distribution
u<-rnorm(1000,0,1) #random numbers from standart normal distribution
y<-5*x+5+u #generating y series
data<-cbind(x,y)
beta1<-c()
beta2<-c()
n<-100 #number of loop#
ksubset<-25 #length of subset#
for (i in 1:n){
datam<-data.frame(data[sample(100,ksubset),])
ols<-lm(y~x,data=datam)
beta1<-append(beta1,ols$coefficients[1])
beta2<-append(beta2,ols$coefficients[2])
}
mean(beta1)
mean(beta2)
par(mfrow=c(2,2))
plot(beta1,main=mean(beta1),type="b",xlab="number of loop",col="blue")
plot(beta2,main=mean(beta2),type="b",xlab="number of loop",col="red")
hist(beta1,freq=FALSE,col="blue")
hist(beta2,freq=FALSE,col="red")