## 'true' parameters
<- 5
beta0_truth <- .25
beta1_truth <- 15
sigma2_truth
## data
<- seq(1000, 1200, length.out = 60)
x <- beta0_truth + x*beta1_truth
mu <- length(x) # total number of observations
n set.seed(3298)
<- rnorm(n, 0, sqrt(sigma2_truth)) # error
epsilon
# observed data
<- mu + epsilon y
Simulation example for day 5 - 08/28/2024
Set the parameters
plot(x, y)
points(x, mu, ty = 'l', col = 'red')
<- data.frame(x= x, y=y) simulated_data
<- lm(y ~ 1 + x, data = simulated_data) m1
coef(m1)
(Intercept) x
8.5594283 0.2467447
confint(m1)
2.5 % 97.5 %
(Intercept) -11.0441489 28.1630056
x 0.2289486 0.2645408
#setting nr of simulations
<- 1000
n_sims # creating the pace to store
<- numeric(length = n_sims)
intercept <- numeric(length = n_sims)
slope <- numeric(length = n_sims)
sigma
for (i in 1:n_sims){
<- rnorm(n, 0, sqrt(sigma2_truth))
e <- mu + e
y <- data.frame(x= x, y=y)
simulated_data <- lm(y ~ 1 + x, data = simulated_data)
m1 <- coef(m1)[1]
intercept[i] <- coef(m1)[2]
slope[i] <- summary(m1)$sigma
sigma[i] }
data.frame(slope) %>%
ggplot(aes(x = slope))+
geom_histogram()+
geom_vline(linetype =2, col = "gold", xintercept = beta1_truth)+
labs(x = TeX("$\\beta_1$"))+
theme_bw()+
theme(aspect.ratio = 1)
data.frame(sigma) %>%
ggplot(aes(x = sigma^2))+
geom_histogram()+
geom_vline(linetype =2, col = "gold", xintercept = sigma2_truth)+
labs(x = TeX("$\\sigma^2$"))+
theme_bw()+
theme(aspect.ratio = 1)
data.frame(slope, intercept) %>%
ggplot(aes(x = slope, y = intercept))+
geom_point()+
labs(x = TeX("$\\beta_1$"),
y = TeX("$\\beta_0$"))+
theme_bw()+
theme(aspect.ratio = 1)
beta1_truth
[1] 0.25
mean(slope)
[1] 0.2500702
sigma2_truth
[1] 15
mean(sigma^2)
[1] 15.04852