Skip to contents

Simulate Data

Suppose we have a simple linear model with two predictors:

n <- 100
X <- cbind(1, rnorm(n), rnorm(n))  # Intercept, X1, X2
beta <- c(2, 1, -1)
sigma <- 1
y <- X %*% beta + rnorm(n, sd = sigma)

OLS Estimation

Estimate the regression coefficients using find_beta:

beta_hat <- find_beta(y, X)
beta_hat
#>        X1        X2        X3 
#>  1.933844  1.107640 -0.751965

Confidence Interval for a Linear Combination

Construct a 95% confidence interval for β2\beta_2 (the coefficient of X1):

gamma <- c(0, 1, 0)  # Selects beta_2
ci_beta2 <- make_CI(y, X, alpha = 0.05, gamma = gamma)
ci_beta2
#> [1] 0.9129346 1.3023464

Constrained Estimation

Suppose we want to estimate the coefficients under the constraint β2+β3=0\beta_2 + \beta_3 = 0:

C <- matrix(c(0, 1, 1), nrow = 1)  # Constraint: beta_2 + beta_3 = 0
d <- 0
beta_hat_H <- find_betaH(y, X, C, d)
beta_hat_H
#>            [,1]
#> [1,]  1.9671298
#> [2,]  0.9359443
#> [3,] -0.9359443

F-test for Linear Restrictions (RSS method)

Test the null hypothesis H0:β2+β3=0H_0: \beta_2 + \beta_3 = 0 using the F-test based on RSS:

f_rss <- f_test_RSS(y, X, C, d)
f_rss
#> $F_statistic
#> [1] 6.174057
#> 
#> $p_value
#> [1] 0.01467634

F-test for Linear Restrictions (Quadratic form)

Alternatively, use the quadratic form for the same test:

f_quad <- f_test_quad(y, X, C, d)
f_quad
#> $F_statistic
#> [1] 6.174057
#> 
#> $p_value
#> [1] 0.01467634