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. 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 the likelihood.
4E2. In the model definition just above, how many parameters are in the posterior distribution?
#There are two parameters μ and σ.
4E3. Using the model definition above, write down the appropriate form of Bayes’ theorem that includes the proper likelihood and priors.
# P(μ, σ|y) =
# [∏iNormal(yi|μ, σ)Normal(μ|0,10)Exponential(1)]/[∫∫∏iNormal(hi|μ, σ)Normal(μ|0,10)Exponential(1)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 is the linear model.
4E5. In the model definition just above, how many parameters are in the posterior distribution?
#There are three parameters α, β and σ.
4M1. For the model definition below, simulate observed y values from the prior (not the posterior). \[\begin{align} \ y_i ∼ Normal(μ, σ) \\ \ μ ∼ Normal(0, 10) \\ \ σ ∼ Exponential(1) \\ \end{align}\]
mu <- rnorm(10000, 0, 10)
sigma <- rexp(10000, 1)
prior_y <- rnorm(10000, mu, sigma)
dens(prior_y)
4M2. Translate the model just above into a quap formula.
flist<- alist(
y ~ dnorm(mu, sigma),
mu ~ dnorm(0, 10),
sigma ~ dexp(1)
)
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.
# h_i ~ Normal(μ, σ)
# μ_i = α + βx_i
# α ∼ Normal(155, 30)
# β ∼ Normal(4, 2)
# σ ~ Uniform(0, 40)
#Assuming the height sample are from students of school age children to college age young adults, in the regression model,
#α: heights for students. For the α prior, I chose a normal distribution with μ=155cm with an SD of 30cm.
#β: yearly growth rates for this wide age range. For the β prior, I chose a normal distribution with mean 4cm/year with an SD of 2cm/year.
#σ prior is uniform distribution 0 to 40cm (wide range for students at both school and college).
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?
#Yes, the β prior is changed as μ=6cm/year with an SD of 1cm/year because every student got taller each year,
# h_i ~ Normal(μ, σ)
# μ_i = α + βx_i
# α ∼ Normal(155, 30)
# β ∼ Normal(6, 1)
# σ ~ Uniform(0, 40)
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?
#Yes, the variance is the square of σ, so if variance is never more than 64cm, then σ prior is never more than 8cm.
# h_i ~ Normal(μ, σ)
# μ_i = α + βx_i
# α ∼ Normal(155, 30)
# β ∼ Normal(6, 1)
# σ ~ 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. What is different? Then compare the posterior predictions of both models.
data(Howell1)
d <- Howell1
d2 <- d[ d$age >= 18, ]
xbar <- mean(d2$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=d2 )
precis(m4.3)
## mean sd 5.5% 94.5%
## a 154.6013670 0.27030767 154.1693632 155.033371
## b 0.9032809 0.04192363 0.8362788 0.970283
## sigma 5.0718810 0.19115479 4.7663788 5.377383
#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=d2 )
precis(m4.3_new)
## mean sd 5.5% 94.5%
## a 114.5342639 1.89775604 111.5012832 117.5672445
## b 0.8907312 0.04175818 0.8239935 0.9574688
## sigma 5.0727425 0.19125117 4.7670862 5.3783988
#After omiting the mean weight xbar, beta is almost the same, but the mean of alpha is now 114.54 (with sd 1.90), lower than mean 154.60 (with sd 0.27) before.
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)
d <- cherry_blossoms
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])
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.
d2 <- Howell1[Howell1$age < 18, ]
nrow(d2)
## [1] 192
#(a)
formula <- alist(
height ~ dnorm(mu, sigma),
mu <- a + b * weight,
a ~ dnorm(110, 30),
b ~ dnorm(0, 10),
sigma ~ dunif(0, 60)
)
m <- map(formula, data = d2)
precis(m, corr = TRUE)
## mean sd 5.5% 94.5%
## a 58.344736 1.39573911 56.114075 60.575397
## b 2.715044 0.06823403 2.605993 2.824095
## sigma 8.437247 0.43057617 7.749103 9.125391
#estimate of a = 58.4: for a participant below 18 years old with a weight of 0 kg the estimate height is 58.4cm.
#estimate of b = 2.72: for each additional unit of weight, there will be an increase in height of around 2.72 cm .
#estimate of σ = 8.44: the standard deviation of heights is around 8.44 cm.
#For every 10 units of increase in weight, the model predicts a 27.2 cm increase in height.
#(b)
plot(height ~ weight, data = d2, col = col.alpha("purple", 0.3))
weight.seq <- seq(from = min(d2$weight), to = max(d2$weight), by = 1)
mu <- link(m, data = data.frame(weight = weight.seq))
mu.mean <- apply(mu, 2, mean)
mu.HPDI <- apply(mu, 2, HPDI, prob = 0.89)
lines(weight.seq, mu.mean)
shade(mu.HPDI, weight.seq)
sim.height <- sim(m, data = list(weight = weight.seq))
height.HPDI <- apply(sim.height, 2, HPDI, prob = 0.89)
shade(height.HPDI, weight.seq)
#(c)
#It seems the linear model does not fit well with the data to predit height, especially when the weight is <10 and >30, the model overestimates the height. I think a polynomial regression might solve this problem.