Week 3 - Basic Regression

This chapter (Chapter 4) introduced the simple linear regression model, a framework for estimating the association between a predictor variable and an outcome variable. The Gaussian distribution comprises the likelihood in such models, because it counts up the relative numbers of ways different combinations of means and standard deviations can produce an observation. To fit these models to data, the chapter introduced quadratic approximation of the posterior distribution and the tool quap. It also introduced new procedures for visualizing prior and posterior distributions.

Place each answer inside the code chunk (grey box). The code chunks should contain a text response or a code that completes/answers the question or activity requested. Make sure to include plots if the question requests them.

Finally, upon completion, name your final output .html file as: YourName_ANLY505-Year-Semester.html and publish the assignment to your R Pubs account and submit the link to Canvas. Each question is worth 5 points.

Questions

4-1. For the model definition below, simulate observed y values from the prior (not the posterior). Make sure to plot the simulation. \[\begin{align} \ y_i ∼ Normal(μ, σ) \\ \ μ ∼ Normal(0, 10) \\ \ σ ∼ Exponential(1) \\ \end{align}\]

set.seed(10)
simu_mu = rnorm(1e4, 0, 10)
simu_sigma = rexp(1e4, 1)
prior_y = rnorm(1e4, simu_mu, simu_sigma)
dens(prior_y, xlab="observed y values")

4-2. Translate the model just above into a quap formula.

quap_formula <- alist(
  y ~ dnorm(mu, sigma),
  mu ~ dnorm(0, 10),
  sigma ~ dexp(1))
4-3. Translate the quap model formula below into a mathematical model definition:

y ~ dnorm( mu , sigma ),
mu <- a + b*x,
a ~ dnorm( 0 , 10 ),
b ~ dunif( 0 , 1 ),
sigma ~ dexp( 1 )

#\[\begin{align} 
# \ y_i ∼ Normal(μ, σ) \\ 
# \ μ_i ∼ α + βx_i \\ 
# \ α ∼ Normal(0, 10) \\ 
# \ β ∼ Uniform(0, 1) \\ 
# \ σ ∼ Exponential(1) \\ 
# \end{align}\]

4-4. A sample of students is measured for height each year for 3 years. After the third year, you want to fit a linear regression predicting height using year as a predictor. Write down the mathematical model definition for this regression, using any variable names and priors you choose. Be prepared to defend your choice of priors. Simulate from the priors that you chose to see what the model expects before it sees the data. Do this by sampling from the priors. Then consider 50 students, each simulated with a different prior draw. For each student simulate 3 years. Plot the 50 linear relationships with height(cm) on the y-axis and year on the x-axis. What can we do to make these priors more likely?

set.seed(10)
simu_alpha <- rnorm(50,130,10)
simu_beta <- rnorm(50,0,10)
simu_sigma <- runif(50, 0,20)

year <- seq(from =1, to=3, len=3)

plot(NULL, xlim=c(1,3),ylim=c(60,200),
     xlab ="Years", ylab = "Height(cm)", main = "Plots : students' height vs year")
for (i in 1:50)
  lines (year, simu_alpha[i] + simu_beta[i] *year , lwd=3 , col=4)

4-5. (a) Now suppose I remind you that every student got taller each year. Does this information lead you to change your choice of priors? How? Again, simulate from the priors and plot. (b) Now suppose I tell you that the variance among heights for students of the same age is never more than 64cm. How does this lead you to revise your priors?

n <- 10000
a <- rnorm(n, 140, 15)
b <- rnorm(n, 0, 15)
sigma <- runif(n, 3, 50)
height <- rnorm(n, a + b * 0, sigma)
dens(height)

# (b)
dens(runif(1000, 0, 8))