set.seed(2020)
N <- 100
x <- rbinom(N, 1, 0.5)
err <- rnorm(N)
y <- 0.5*x + err
G0 <- glm(y~1, family="gaussian")
G1 <- glm(y~x, family="gaussian")
test_stat <- summary(G0)$deviance - summary(G1)$deviance
df <- dim(summary(G1)$coefficients)[1] - dim(summary(G0)$coefficients)[1]
p.value <- 1-pchisq(test_stat, df)
p.value
summary(G1)$coefficients[2,4]
set.seed(2020)
N <- 100
x <- rbinom(N, 1, 0.5)
err <- rnorm(N)
y <- 0.5*x + err
G0 <- glm(y ~ 1, family="gaussian")
G1 <- glm(y ~ x, family="gaussian")
test_stat <- summary(G0)$deviance -
summary(G1)$deviance
df <- dim(summary(G1)$coefficients)[1] -
dim(summary(G0)$coefficients)[1]
p.value <- 1-pchisq(test_stat, df)
p.value
summary(G1)$coefficients[2, 4]
set.seed(2020)
N <- 100 # sample size
R <- 1000 # number of simulations
alpha <- 0.05
lrt_r <- wld_r <- rep(0,R)
for (i in 1:R){
x <- rbinom(N, 1, 0.5)
err <- rnorm(N)
y <- 0.5*x + err
G0 <- glm(y~1, family="gaussian") # intercept-only model
G1 <- glm(y~x, family="gaussian") # model with x as a predictor
test_stat <- summary(G0)$deviance - summary(G1)$deviance
df <- dim(summary(G1)$coefficients)[1] - dim(summary(G0)$coefficients)[1]
lr <- 1-pchisq(test_stat, df) # likelihood ratio test p-value
wd <- summary(G1)$coefficients[2,4] # Wald test p-value
# check if the p-values warrant rejection at the level of alpha
#
if (lr <= alpha){lrt_r[i] <- 1}
if (wd <= alpha){wld_r[i] <- 1}
}
# Check the power of each test
#
sum(lrt_r)/R*100 # 70.4%
sum(wld_r)/R*100 # 69.9%
set.seed(2020)
N <- 100 # sample size
R <- 1000 # number of simulations
alpha <- 0.05
lrt_r <- wld_r <- rep(0,R)
for (i in 1:R){
x <- rbinom(N, 1, 0.5)
err <- rnorm(N)
y <- 0.5*x + err
G0 <- glm(y ~ 1, family="gaussian")
# intercept-only model
G1 <- glm(y ~ x, family="gaussian")
# model with x as a predictor
test_stat <- summary(G0)$deviance -
summary(G1)$deviance
df <- dim(summary(G1)$coefficients)[1] -
dim(summary(G0)$coefficients)[1]
lr <- 1-pchisq(test_stat, df)
# likelihood ratio test p-value
wd <- summary(G1)$coefficients[2, 4]
# Wald test p-value
# check if the p-values warrant rejection at the level of alpha
#
if (lr <= alpha){lrt_r[i] <- 1}
if (wd <= alpha){wld_r[i] <- 1}
}
# Check the power of each test
#
sum(lrt_r)/R*100 # 70.4%
sum(wld_r)/R*100 # 69.9%