Chapter 4 - Geocentric Models

This chapter 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}\]
draw <- tibble(mu = rnorm(n = 20000, mean = 0, sd = 10),
               sigma = rexp(n = 20000, rate = 1)) %>%
              mutate(y = rnorm(n = 20000, mean = mu, sd = sigma))

ggplot(draw, aes(x = y)) +
  geom_density()

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

flist <- 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 )

# y_i ∼ Normal(μi, σ) 
# μ_i = a + bxi
# a ∼ Normal(0, 10) 
# b ~ Uniform(0, 1)
# σ ∼ Exponential(1)

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?

# yi ∼ Normal(μi,σ)
# μi = α + βti
# α ∼ Normal(160,20)
# β ∼ Normal(5,2)
# σ ∼ Uniform(0,50)
# Assume the 160cm is the center of the height normal distribution, and the SD is 20cm. β: assume the average growth is 5cm/year with 2cm SD.
N <- 50
a <- rnorm( N, 160, 20 )
b <- rnorm( N, 5, 2 )
sigma <- runif( N, 0, 50 )
height_0 <- rnorm( N, a + b * 0, sigma )
dens( height_0 )

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?

# No, because the prior β above is already > 0
# σ ∼ Uniform(0,80)

4-6. In the chapter, we used 15 knots with the cherry blossom spline. Increase the number of knots and observe what happens to the resulting spline. Then adjust also the width of the prior on the weights—change the standard deviation of the prior and watch what happens. What do you think the combination of knot number and the prior on the weights controls?

data(cherry_blossoms) 
d <- cherry_blossoms
str(cherry_blossoms)
## 'data.frame':    1215 obs. of  5 variables:
##  $ year      : int  801 802 803 804 805 806 807 808 809 810 ...
##  $ doy       : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ temp      : num  NA NA NA NA NA NA NA NA NA NA ...
##  $ temp_upper: num  NA NA NA NA NA NA NA NA NA NA ...
##  $ temp_lower: num  NA NA NA NA NA NA NA NA NA NA ...
d2 <- d[ complete.cases(d$temp) , ]
num_knots <- 30
knot_list <- quantile( d2$year , probs=seq(0,1,length.out=num_knots))
library(splines) 
B <- bs(d2$year, knots=knot_list[-c(1,num_knots)] , degree=3 , intercept=TRUE )
plot( NULL , xlim=range(d2$year) , ylim=c(0,1) , xlab="year" , ylab="value" )
for ( i in 1:ncol(B) ) lines( d2$year , B[,i])