Docsity

Econometrics assignment , Assignments of Computer science

Toggle sidebar
bg1
Econometrics 1, Computer Class week 3
STUDENT NAME & NUMBER
today
# betatrue parameter under H0 (do both a(i) and b(i) together since DGP is the same)
# Initialize variables for the loop to make it run faster
nrsim <- 10000
t_simulated <- matrix(0, nrsim, k)
# DGP y <- X*betatrue + eps
# repeat 1000 (reps) times
# draw sample of size n,
# calculate y, b and s2 and t
# store in a matrix
# keep X and beta fixed (so do not change inside the loop)
# draw new eps inside the loop
for (rr in 1:nrsim) {
eps <- rnorm(n, 0, sig) # draw new eps from normal distributions
y<- X %*% betatrue + eps # calculate new y's
betahat <- XXinvX %*% y # calculate new betahat for this sample
e<- y-X%*%betahat
s2 <- sum(e*e)/(n-k)
seb <- sqrt(s2)%*%sqrtdiagXXinv # this contains the s.e. of all k coefficients
# use betahat and seb as vectors to divide element-wise
tstat <- c(betahat)/c(seb) # this contains the t-statistics of each coefficient
t_simulated[rr,] <-tstat
}
# for question a(1) plot the distribution of t_2
par(mfrow = c(1,2))
hist(t_simulated[,2], 30,freq = FALSE) # using 30 bins relative freq distribution
# for question b(1) calculate the rejection probability
critical_value <- qt(0.975,n-k) # critical value based on Student t-distribution
# for 5% two-sided test or a 2.5% one sided test
rejectH0 <- (abs(t_simulated[,2])>critical_value) # reject if |t|>critical_value
rej_p_under_H0 <- mean(rejectH0)
sprintf('The rejection probability under H0 is %0.5g', rej_p_under_H0)
## [1] " The rejection probability under H0 is 0.0511"
1
pf3

Partial preview of the text

Download Econometrics assignment and more Assignments Computer science in PDF only on Docsity!

Econometrics 1, Computer Class week 3

STUDENT NAME & NUMBER

today

# betatrue parameter under H0 (do both a(i) and b(i) together since DGP is the same)

# Initialize variables for the loop to make it run faster nrsim <- 10000 t_simulated <- matrix(0, nrsim, k)

_# DGP y <- X*betatrue + eps

repeat 1000 (reps) times

draw sample of size n,

calculate y, b and s2 and t

store in a matrix

keep X and beta fixed (so do not change inside the loop)

draw new eps inside the loop_

for (rr in 1:nrsim) { eps <- rnorm(n, 0, sig) # draw new eps from normal distributions y <- X %% betatrue + eps # calculate new y's betahat <- XXinvX %% y # calculate new betahat for this sample e <- y-X%%betahat s2 <- sum(ee)/(n-k) seb <- sqrt(s2)%*%sqrtdiagXXinv # this contains the s.e. of all k coefficients

# use betahat and seb as vectors to divide element-wise tstat <- c(betahat)/c(seb) # this contains the t-statistics of each coefficient t_simulated[rr,] <-tstat }

# for question a(1) plot the distribution of t_ par(mfrow = c(1, 2)) hist(t_simulated[,2], 30, freq = FALSE) # using 30 bins relative freq distribution

# for question b(1) calculate the rejection probability critical_value <- qt(0.975,n-k) _# critical value based on Student t-distribution

for 5% two-sided test or a 2.5% one sided test_

rejectH0 <- (abs(t_simulated[,2])>critical_value) # reject if |t|>critical_value rej_p_under_H0 <- mean(rejectH0)

sprintf(' The rejection probability under H0 is %0.5g', rej_p_under_H0)

[1] " The rejection probability under H0 is 0.0511"

Histogram of t_simulated[, 2]

t_simulated[, 2]

Density

_# Repeat whole process under the alternative Ha

ii. beta2 = 1 (this is the distribution under Ha : beta2 = 1)_

########################################################################### betatrue <- c(1, 1) # beta parameter under alternative

t_simulated <- matrix(0, nrsim, k) # start with zero matrix

for (rr in 1:nrsim) { eps <- rnorm(n, 0, sig) # draw new eps from normal distributions y <- X %% betatrue + eps # calculate new y's betahat <- XXinvX %% y # calculate new betahat for this sample e <- y-X%%betahat s2 <- sum(ee)/(n-k) seb <- sqrt(s2)%*%sqrtdiagXXinv # this contains the s.e. of all k coefficients

# use betahat and seb as vectors to divide element-wise tstat <- c(betahat-1)/c(seb) # this contains the t-statistics of each coefficient t_simulated[rr,] <- tstat }

# for question a(2) plot Ha histogram in a second figure hist(t_simulated[,2], 30, freq = FALSE) # using 30 bins