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}\]

mu_model = rnorm(10000, 0, 10)
sigma_model = rexp(10000, 1)
prior_model = rnorm(10000, mu_model, sigma_model)

par(mfrow = c(1,1))
dens(prior_model, main = "Density plot for the Prior of the model")

hist(prior_model, xlab = "Prior of the model", ylab = "Frequency", main = "Histrogram of the Prior", color = "light grey", breaks = 20)

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

quap_form = 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 = α + β * x_i
#α ∼ Normal(0, 10)
#β ∼ 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?

alpha = rnorm(1000, 150, 25)

par(mfrow = c(1,1))
dens(alpha, main = "Density plot of linear relationship between Height and Year")

hist(alpha, main = "Histogram of linear relationship between Height and Year", xlab = "Years of evaluation", ylab = "Height in cm", breaks = 20)

ANS: To fit linear regression we will use alpha function to normalize mu and sigma values to produce the relation between height and year.

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.

  1. 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?
#when we consider the fact that every student got taller each year, we need to change the prior using log normal distribution.

beta = rlnorm(1000, 2, 0.5)

par(mfrow = c(1,1))
dens(beta, main = "Density plot of linear relationship between Height and Year")

hist(beta, main = "Histogram of linear relationship between Height and Year", xlab = "Years of evaluation", ylab = "Height in cm", breaks = 20)

# variance = square(sigma)
# so when we say that he variance among heights for students of the same age is never more than 64cm that signfies 
# sigma = square root(variance) i.e. sigma = square root(64) = 8

sigma = runif(10000, 0,8)

par(mfrow = c(1,1))
dens(sigma, main = "Density plot of linear relationship between Height and Year")

hist(sigma, main = "Histogram of linear relationship between Height and Year", xlab = "Years of evaluation", ylab = "Height in cm", breaks = 20)

ANS:

When we consider the fact that every student got taller each year, we need to change the prior using log normal distribution. variance = square(sigma) So when we say that he variance among heights for students of the same age is never more than 64cm that signfies sigma = square root(variance) i.e. sigma = square root(64) = 8

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?

library(splines) 

data(cherry_blossoms)
cherry_data1 = cherry_blossoms
precis(cherry_data1)
##                   mean          sd      5.5%      94.5%
## year       1408.000000 350.8845964 867.77000 1948.23000
## doy         104.540508   6.4070362  94.43000  115.00000
## temp          6.141886   0.6636479   5.15000    7.29470
## temp_upper    7.185151   0.9929206   5.89765    8.90235
## temp_lower    5.098941   0.8503496   3.78765    6.37000
##                                                                                                                           histogram
## year                       <U+2587><U+2587><U+2587><U+2587><U+2587><U+2587><U+2587><U+2587><U+2587><U+2587><U+2587><U+2587><U+2581>
## doy                                                                <U+2581><U+2582><U+2585><U+2587><U+2587><U+2583><U+2581><U+2581>
## temp                                                               <U+2581><U+2583><U+2585><U+2587><U+2583><U+2582><U+2581><U+2581>
## temp_upper <U+2581><U+2582><U+2585><U+2587><U+2587><U+2585><U+2582><U+2582><U+2581><U+2581><U+2581><U+2581><U+2581><U+2581><U+2581>
## temp_lower <U+2581><U+2581><U+2581><U+2581><U+2581><U+2581><U+2581><U+2583><U+2585><U+2587><U+2583><U+2582><U+2581><U+2581><U+2581>
cherry_data2 = cherry_data1[complete.cases(cherry_data1$temp), ]

cherry_spline <- function(n_Knots, StdV) {
 
  knot_list <- quantile(cherry_data2$year, probs = seq(0, 1, length.out = n_Knots))[-c(1, n_Knots)]
 
  B <- bs(cherry_data2$year,
    knots = knot_list,
    degree = 3, intercept = TRUE
  )
  # Run quap model
  m4.7 <- quap(alist(
    T ~ dnorm(mu, sigma),
    mu <- a + B %*% w,
    a ~ dnorm(6, 10),
    w ~ dnorm(0, StdV),
    sigma ~ dexp(1)
  ),
  data = list(T = cherry_data2$temp, B = B, StdV = StdV),
  start = list(w = rep(0, ncol(B)))
  )
  
  mu <- link(m4.7)
  mu_PI <- apply(mu, 2, PI, 0.97)
  plot(cherry_data2$year, cherry_data2$temp,
    col = col.alpha(rangi2, 0.3), pch = 16,
    main = "Changes in plot by changing knots, width of the prior and standard deviation"
  )
  shade(mu_PI, cherry_data2$year, col = col.alpha("black", 0.5))
}

cherry_spline(n_Knots = 25, StdV = 1) #changing knots = 25 #Increasing the knots means more wiggle in global function i.e. it makes function more flexible.

cherry_spline(n_Knots = 25, StdV = 50) #changing standard deviation = 50 #Increasing the standard deviation along prior adds more sinuous curves in the plot

cherry_spline(n_Knots = 25, StdV = 0.5) #changing standard deviation = 0.5 #Decrasing the standard deviation along prior reduces the sinuous curves in the plot and making it smoother as compared to increased standard deviation.

ANS: Increasing the knots means more wiggle in global function i.e. it makes function more flexible.Increasing the standard deviation along prior adds more sinuous curves in the plot and decrasing the standard deviation along prior reduces the sinuous curves in the plot and making it smoother as compared to increased standard deviation.