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. Problems are labeled Easy (E), Medium (M), and Hard(H).
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.
4E1. In the model definition below, which line is the likelihood? \[\begin{align} \ y_i ∼ Normal(μ, σ) \\ \ μ ∼ Normal(0, 10) \\ \ σ ∼ Exponential(1) \\ \end{align}\]
## y_i ∼ Normal(μ, σ) is likelihood
4E2. In the model definition just above, how many parameters are in the posterior distribution?
## There are 2 parameters (μ, σ) in posterior distribution
4E3. Using the model definition above, write down the appropriate form of Bayes’ theorem that includes the proper likelihood and priors.
##Bayes Theorem P(a|b) = {[P(b|a) * P(a)]/P(b)}
# posterior = (likelihood * prior)/margin
# P(μ, σ|y) =
# {[∏iNormal(yi|μ, σ)Normal(μ|0,10)Uniform(σ|0,10)i]/∫∫∏iNormalhi|μ, σ)Normal(μ|0,10)Uniform(σ|0,10)dμdσ}
4E4. In the model definition below, which line is the linear model? \[\begin{align} \ y_i ∼ Normal(μ, σ) \\ \ μ_i = α + βx_i \\ \ α ∼ Normal(0, 10) \\ \ β ∼ Normal(0, 1) \\ \ σ ∼ Exponential(2) \\ \end{align}\]
## μ_i = α + βx_i
4E5. In the model definition just above, how many parameters are in the posterior distribution?
## α, β, σ
4M1. 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 = rnorm(1e4, 0, 10)
sigma = runif(1e4, 0, 10)
prior_y = rnorm(1e4, mu, sigma)
dens(prior_y)
4M2. Translate the model just above into a quap formula.
quap_formula = alist(
y ~ dnorm(mu, sigma),
mu ~ dnorm(0, 10),
sigma ~ dunif(0, 10)
)
4M3. 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)
4M4. 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?
## As we want to fit a linear regression and predict height of students using year as predictor, We will use Linear model.
## First we will consider age group of students to be 6 to 18 years
## We will consider height distribution to be 110 cm to 170 cm and normal distribution centered on 140cm with Standard Deviation 25cm.
## For Beta prior, Normal distribution is considered to be 4cm/year and Standard Deviation 2cm/year
## For Sigma prior, uniform distribution is considered to be 0cm to 50cm
## y_i ~ Normal(μ, σ)
## μ_i = α + βx_i
## α ∼ Normal(140, 25)
## β ∼ Normal(4, 2)
## σ ~ Uniform(0, 50)
4M5. 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.
## After getting information about students getting taller every year we will change normal distribution to be Normal(125, 10) and Beta is changed to Normal(7,1)
## y_i ~ Normal(μ, σ)
## μ_i = α + βx_i
## alpha∼Normal(125, 10)
## beta∼Normal(7,1)
## sigma∼Uniform(0,50)
4M6. 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?
## As variance < 64 then sigma will not be greater than 8 (sqrt(variance))
# Sigma prior is revised to sigma∼Uniform(0,8)
## y_i ~ Normal(μ, σ)
## μ_i = α + βx_i
## alpha∼Normal(125, 10)
## beta∼Normal(7,1)
## sigma∼Uniform(0,8)
4M7. Refit model m4.3 from the chapter, but omit the mean weight xbar this time. Compare the new model’s posterior to that of the original model. In particular, look at the covariance among the parameters. Show the pairs() plot. What is different? Then compare the posterior predictions of both models.
data(Howell1)
data <- Howell1
data2<- data[data$age>=18,]
xbar <- mean(data2$weight)
m4.3 <- quap( alist(height ~ dnorm( mu , sigma ) , mu <- a + b*( weight - xbar ) ,
a ~ dnorm( 178 , 20 ) , b ~ dlnorm( 0 , 1 ) , sigma ~ dunif( 0 , 50 )) , data=data2)
precis(m4.3)
## mean sd 5.5% 94.5%
## a 154.6013675 0.27030764 154.1693637 155.033371
## b 0.9032809 0.04192363 0.8362788 0.970283
## sigma 5.0718805 0.19115475 4.7663783 5.377383
m4.3
##
## Quadratic approximate posterior distribution
##
## Formula:
## height ~ dnorm(mu, sigma)
## mu <- a + b * (weight - xbar)
## a ~ dnorm(178, 20)
## b ~ dlnorm(0, 1)
## sigma ~ dunif(0, 50)
##
## Posterior means:
## a b sigma
## 154.6013675 0.9032809 5.0718805
##
## Log-likelihood: -1071.01
## Ommitting xbar
m4.3_new <- quap(alist(height ~ dnorm( mu , sigma ) , mu <- a + b*( weight ) ,
a ~ dnorm( 178 , 20 ) , b ~ dlnorm( 0 , 1 ) , sigma ~ dunif( 0 , 50 )), data=data2 )
precis(m4.3_new)
## mean sd 5.5% 94.5%
## a 114.5343089 1.89774562 111.5013449 117.5672729
## b 0.8907302 0.04175795 0.8239929 0.9574674
## sigma 5.0727154 0.19124861 4.7670632 5.3783676
m4.3_new
##
## Quadratic approximate posterior distribution
##
## Formula:
## height ~ dnorm(mu, sigma)
## mu <- a + b * (weight)
## a ~ dnorm(178, 20)
## b ~ dlnorm(0, 1)
## sigma ~ dunif(0, 50)
##
## Posterior means:
## a b sigma
## 114.5343089 0.8907302 5.0727154
##
## Log-likelihood: -1071.07
# Using same weight, beta remains the same, but for alpha mean get decreased and SD get increased
4M8. 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)
data <- cherry_blossoms
data2 <- data[ complete.cases(data$temp) , ]
num_knots <- 30
knot_list <- quantile( data2$year , probs=seq(0,1,length.out=num_knots))
library(splines)
B <- bs(data2$year, knots=knot_list[-c(1,num_knots)] , degree=3 , intercept=TRUE )
plot( NULL , xlim=range(data2$year) , ylim=c(0,1) , xlab="year" , ylab="value" )
for ( i in 1:ncol(B) ) lines( data2$year , B[,i])
4H2. Select out all the rows in the Howell1 data with ages below 18 years of age. If you do it right, you should end up with a new data frame with 192 rows in it.
Fit a linear regression to these data, using quap. Present and interpret the estimates. For every 10 units of increase in weight, how much taller does the model predict a child gets?
Plot the raw data, with height on the vertical axis and weight on the horizontal axis. Superimpose the MAP regression line and 89% interval for the mean. Also superimpose the 89% interval for predicted heights.
What aspects of the model fit concern you? Describe the kinds of assumptions you would change, if any, to improve the model. You don’t have to write any new code. Just explain what the model appears to be doing a bad job of, and what you hypothesize would be a better model.
H2 <- Howell1[Howell1$age < 18, ]
nrow(H2)
## [1] 192
fit <- alist(height ~ dnorm(mu, sigma), mu <- a + b * weight,
a ~ dnorm(110, 30), b ~ dnorm(0, 10),sigma ~ dunif(0, 60))
fit2 <- quap(fit, data = H2)
precis(fit2)
## mean sd 5.5% 94.5%
## a 58.344922 1.39564956 56.11440 60.575440
## b 2.715035 0.06822965 2.60599 2.824079
## sigma 8.436703 0.43050679 7.74867 9.124736
plot(height ~ weight, data = H2, col = col.alpha("purple", 0.25))
weight.sq <- seq(from = min(H2$weight), to = max(H2$weight), by = 1)
mu <- link(fit2, data = data.frame(weight = weight.sq))
mu.mean = apply(mu, 2, mean)
mu.HPDI = apply(mu, 2, HPDI, prob = 0.89)
# draw MAP line
lines(weight.sq, mu.mean)
# draw HPDI region for line
shade(mu.HPDI, weight.sq)
# simulate posterior observations of the model fit
sim.height <- sim(fit2, data = list(weight = weight.sq))
height.HPDI <- apply(sim.height, 2, HPDI, prob = 0.89)
shade(height.HPDI, weight.sq)
## Data points are not fitting regression line well, more specifically model under estimates height when weight lower than 10 or greater than 35
## We need to think about using polynomial regression as our assumption of linear relationship between mu and weight is wrong. There might be more variables contributing to the prediction.