

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


# 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
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
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)
_# Repeat whole process under the alternative Ha
########################################################################### 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