Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Assignment: Monte Carlo Simulations 1

1. CODE:
# LECTURE 6 STAT 106
#Rishav Chakrabarti, roll no. = 1
#Assignment
#a)
x= seq(90:110) #stores a vector of 21 values from 90 to 110 in x
length(x) #gives length of x
y= x + rnorm(length(x)) #randomises y w.r.t x by adding 21 random values from std. normal
distn
plot(y~x) #plots y on x
lm(y~x) #gives regression of y on x

y= x + rnorm(length(x)) #randomizes once again


lm(y~x) #regression model of y on x
plot(y~x) #plots y on x
abline(lm(y~x)) #plots regression line of y on x

slopesamp= function(N){
slope= numeric(N)
for (i in 1:N) {
y=x + rnorm(length(x))
y.lm= lm(y~x)
slope[[i]] = as.vector(y.lm$coefficients[2])
}
slope
} #defines a function based on sample size which gives least square estimates of the slope
slopesamp(50) #slopes for samp size 50
plot(density(slopesamp(50))) #plots density of slope for samp size 50
mean(slopesamp(50)) #mean of slope for samp size 50
sd(slopesamp(50)) #std dev of slope for samp size 50
sd(slopesamp(50))/sqrt(50) #gives std error for slope estimator for samp size 50

#b)
#we study the means and standard errors of the slope depending on the sample size

mean(slopesamp(50))
sd(slopesamp(50))/sqrt(50)

mean(slopesamp(100))
sd(slopesamp(100))/sqrt(100)

mean(slopesamp(200))
sd(slopesamp(200))/sqrt(200)

mean(slopesamp(400))
sd(slopesamp(400))/sqrt(400)

mean(slopesamp(800))
sd(slopesamp(800))/sqrt(800)

mean(slopesamp(1000))
sd(slopesamp(1000))/sqrt(1000)
#gives means and standard errors of slope estimator for sample sizes 50, 100, 200, 400, 800
and 1000 respectively
#we see that the mean is approximately 1 and the standard error keeps decreasing
progressively with the increase in sample size
#c)

varsamp = function(N){
sigma2= numeric(N)
for (i in 1:N) {
y=x + rnorm(length(x))
y.lm= lm(y~x)
sigma2[[i]] = as.vector((summary(y.lm)$sigma)^2)
}
sigma2
}
#defines a function based on sample size which gives least square estimates of the variance

varsamp(50) #variances for samp size 50

mean(varsamp(50))
sd(varsamp(50))/sqrt(50)

mean(varsamp(100))
sd(varsamp(100))/sqrt(100)

mean(varsamp(200))
sd(varsamp(200))/sqrt(200)

mean(varsamp(400))
sd(varsamp(400))/sqrt(400)

mean(varsamp(800))
sd(varsamp(800))/sqrt(800)
mean(varsamp(1000))
sd(varsamp(1000))/sqrt(1000)
#gives means and standard errors of LS variance estimator for sample sizes 50, 100, 200,
400, 800 and 1000 respectively
#we see that the mean is approximately 1 and the variance decreases progressively with the
increase in sample size

2. OUTPUT
> # LECTURE 6 STAT 106
> #Rishav Chakrabarti, roll no. = 1
> #Assignment
> #a)
> x= seq(90:110) #stores a vector of 21 values from 90 to 110 in x
> length(x) #gives length of x
[1] 21
> y= x + rnorm(length(x)) #randomises y w.r.t x by adding 21 random values from std.
normal distn
> plot(y~x) #plots y on x
> lm(y~x) #gives regression of y on x

Call:
lm(formula = y ~ x)

Coefficients:
(Intercept) x
0.1499 0.9770

>
> y= x + rnorm(length(x)) #randomizes once again
> lm(y~x) #regression model of y on x

Call:
lm(formula = y ~ x)

Coefficients:
(Intercept) x
-0.6563 1.0300

> plot(y~x) #plots y on x


> abline(lm(y~x)) #plots regression line of y on x
>
> slopesamp= function(N){
+ slope= numeric(N)
+ for (i in 1:N) {
+ y=x + rnorm(length(x))
+ y.lm= lm(y~x)
+ slope[[i]] = as.vector(y.lm$coefficients[2])
+ }
+ slope
+ } #defines a function based on sample size which gives least square estimates of the slope
>
> slopesamp(50) #slopes for samp size 50
[1] 0.9942725 1.0301512 0.9711009 1.0198058 0.9844931 1.0182386 0.9660818
0.9751058 0.9730166 1.0300373 0.9589829 0.9399774
[13] 0.9963138 1.0293072 0.9893063 0.9752135 0.9415567 0.9885444 1.0268508
1.0171025 0.9455796 0.9531407 1.0116785 0.9770020
[25] 0.9904648 0.9504994 0.9339421 1.0391057 0.9946880 0.9763624 0.9246582
0.9868652 0.9616129 0.9772203 0.9696472 1.0192741
[37] 0.9968730 0.9937702 1.0401287 1.0376028 0.9774488 1.0220372 1.0275576
0.9644600 1.0207613 0.9659544 1.0406574 1.0129566
[49] 0.9734276 1.0335846
> plot(density(slopesamp(50))) #plots density of slope for samp size 50
> mean(slopesamp(50)) #mean of slope for samp size 50
[1] 0.9938124
> sd(slopesamp(50)) #std dev of slope for samp size 50
[1] 0.02909653
> sd(slopesamp(50))/sqrt(50) #gives std error for slope estimator for samp size 50
[1] 0.004505562
>
> #b)
> #we study the means and standard errors of the slope depending on the sample size
>
> mean(slopesamp(50))
[1] 0.9919371
> sd(slopesamp(50))/sqrt(50)
[1] 0.00573147
>
> mean(slopesamp(100))
[1] 0.9988957
> sd(slopesamp(100))/sqrt(100)
[1] 0.004124355
>
> mean(slopesamp(200))
[1] 0.9986381
> sd(slopesamp(200))/sqrt(200)
[1] 0.002292578
>
> mean(slopesamp(400))
[1] 0.9985969
> sd(slopesamp(400))/sqrt(400)
[1] 0.001808386
>
> mean(slopesamp(800))
[1] 1.000694
> sd(slopesamp(800))/sqrt(800)
[1] 0.001191938
>
> mean(slopesamp(1000))
[1] 1.002376
> sd(slopesamp(1000))/sqrt(1000)
[1] 0.001144198
> #gives means and standard errors of slope estimator for sample sizes 50, 100, 200, 400,
800 and 1000 respectively
> #we see that the mean is approximately 1 and the standard error keeps decreasing
progressively with the increase in sample size
>
> #c)
>
> varsamp = function(N){
+ sigma2= numeric(N)
+ for (i in 1:N) {
+ y=x + rnorm(length(x))
+ y.lm= lm(y~x)
+ sigma2[[i]] = as.vector((summary(y.lm)$sigma)^2)
+ }
+ sigma2
+}
> #defines a function based on sample size which gives least square estimates of the
variance
>
> varsamp(50) #variances for samp size 50
[1] 1.0926973 1.0332384 1.0079838 1.5984259 0.7815795 0.8679718 0.3255494
0.6378475 1.4353410 0.5080900 1.6711782 0.7510396
[13] 0.9010026 0.6792927 1.0585068 0.6117230 0.8342699 0.5577675 1.4754616
0.6016926 2.1725521 1.2479130 0.8783213 1.1072473
[25] 0.8830348 1.0512820 1.0801212 1.3786313 0.6845370 0.7020119 1.3069577
1.0452364 1.2329156 1.0279941 1.4045570 0.9557426
[37] 0.6118152 1.1701059 1.0341373 0.6581685 0.8229168 1.3145374 1.3499489
0.9438968 1.0665591 1.7364729 0.7626371 1.0255028
[49] 1.5744348 1.6651833
>
> mean(varsamp(50))
[1] 1.070846
> sd(varsamp(50))/sqrt(50)
[1] 0.04840657
>
> mean(varsamp(100))
[1] 1.008759
> sd(varsamp(100))/sqrt(100)
[1] 0.03426253
>
> mean(varsamp(200))
[1] 0.9721393
> sd(varsamp(200))/sqrt(200)
[1] 0.02279034
>
> mean(varsamp(400))
[1] 0.9903518
> sd(varsamp(400))/sqrt(400)
[1] 0.01560948
>
> mean(varsamp(800))
[1] 0.995398
> sd(varsamp(800))/sqrt(800)
[1] 0.0115971
>
> mean(varsamp(1000))
[1] 0.9902257
> sd(varsamp(1000))/sqrt(1000)
[1] 0.01067279
> #gives means and standard errors of LS variance estimator for sample sizes 50, 100, 200,
400, 800 and 1000 respectively
> #we see that the mean is approximately 1 and the variance decreases progressively with
the increase in sample size
3. PLOTS

You might also like