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}\]
answer: yi∼Normal(μ,σ)
4E2. In the model definition just above, how many parameters are in the posterior distribution?
answer: 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.
answer: pr(μ,σ|y) = ΠiNormal(yi|μ,σ)Normal(μ|0,10)Uniform(σ|0,10)/ ∫∫ΠiNormal(hi|μ,σ)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}\]
answer:
the μ_i = α + βx_i \ is the linear model
the first one y_i ∼ Normal(μ, σ) \ is the likelihood
the third one α ∼ Normal(0, 10) \ is the prior of σ
the fourth one β ∼ Normal(0, 1) \ is the prior of β
the fifth one σ ∼ Exponential(2) \ is the prior of σ
4E5. In the model definition just above, how many parameters are in the posterior distribution?
answer:
answer: There are two parameters α, β,and σ
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}\]
s_mu <- rnorm(1e4, 0, 10)
s_sigma <- runif(1e4, 0, 10)
prior_y <- rnorm(1e4, s_mu, s_sigma)
dens(prior_y)
4M2. Translate the model just above into a quap formula.
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) \\
# \ β ∼ Normal(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?
# \ y_i ∼ Normal(μ, σ) \\
# \ μ_i = α + βx_i \\
# \ α ∼ Normal(160, 20) \\
# \ β ∼ Normal(3, 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.
# if we know that every student got taller, we can think that the height is not height, we can lower the mean. For instance, we can lower the standard deviation from 20 to 10. the β can increase an around 5cm per year and 1cm per year. max value for σ prior can be reduce from 50 to 10
# \ y_i ∼ Normal(μ, σ) \\
# \ μ_i = α + βx_i \\
# \ α ∼ Normal(120, 10) \\
# \ β ∼ Normal(5, 1) \\
# \ σ ∼ Uniform(0,10) \\
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?
## because of σ^2 <= 64
# \ y_i ∼ Normal(μ, σ) \\
# \ μ_i = α + βx_i \\
# \ α ∼ Normal(120, 10) \\
# \ β ∼ Normal(5, 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. Show the pairs() plot. What is different? Then compare the posterior predictions of both models.
data("Howell1")
d<- Howell1
d2<- d[d$age >=18,]
xbar<- mean(d2$weight)
# build moel
m43<-quap(
alist(
height ~ dnorm(mu, sigma),
mu <- a + b*(weight - xbar),
a ~ dnorm(160,20),
b ~ dnorm(0,1),
sigma ~ dunif(0,50)
),
data=d2)
precis(m43)
## mean sd 5.5% 94.5%
## a 154.5980793 0.27030714 154.1660763 155.0300824
## b 0.9034409 0.04189133 0.8364904 0.9703913
## sigma 5.0718746 0.19115411 4.7663734 5.3773757
# without weight
m43.1<-quap(
alist(
height ~ dnorm(mu, sigma),
mu <- a + b*weight,
a ~ dnorm(160,20),
b ~ dnorm(0,1),
sigma ~ dunif(0,50)
),
data=d2)
precis(m43.1)
## mean sd 5.5% 94.5%
## a 114.3632003 1.89589219 111.3331984 117.3932022
## b 0.8944425 0.04171667 0.8277712 0.9611138
## sigma 5.0722157 0.19119445 4.7666501 5.3777814
answer: we can see the β and σ are still the same value, but α is decreased.
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
precis(d)
## 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>
d3 <- d[complete.cases(d$doy),]
numknots <- 15
knot_list <- quantile(d3$year, probs=seq(0, 1, length.out=numknots))
knot_list
## 0% 7.142857% 14.28571% 21.42857% 28.57143% 35.71429% 42.85714% 50%
## 812 1036 1174 1269 1377 1454 1518 1583
## 57.14286% 64.28571% 71.42857% 78.57143% 85.71429% 92.85714% 100%
## 1650 1714 1774 1833 1893 1956 2015
library(splines)
B <- bs(d3$year,knots=knot_list[-c(1,numknots)], degree=3, intercept = TRUE)
plot(NULL, xlim=range(d3$year), ylim=c(0,1), ylab="basis")
for (i in 1:ncol(B)) lines(d3$year, B[,i])
# Increase the number of knots
numknots2 <-30
knot_list2 <- quantile(d3$year, probs=seq(0,1,length.out=numknots2))
B2 <- bs(d3$year,knots=knot_list2[-c(1,numknots2)], degree=3, intercept = TRUE)
plot(NULL, xlim=range(d3$year), ylim=c(0,1), ylab="basis")
for (i in 1:ncol(B2)) lines(d3$year, B2[,i])
answer: we can see the curve of 30 knots is wigglier than the curve with 15 knots.
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.
data(Howell1)
set.seed(3)
d3 <- Howell1[Howell1$age < 18, ]
nrow(d3)
## [1] 192
formula <- alist(
height ~ dnorm(mu, sigma),
mu <- a + b * weight,
a ~ dnorm(140, 10),
b ~ dnorm(0, 10),
sigma ~ dunif(0, 60)
)
m <- quap(formula, data = d3)
precis(m, corr = TRUE)
## mean sd 5.5% 94.5%
## a 59.808786 1.39731555 57.575606 62.041966
## b 2.650637 0.06833953 2.541418 2.759857
## sigma 8.465089 0.43480623 7.770184 9.159993
plot(height ~ weight, data = d3, col = col.alpha("steel blue", 0.8))
weight.seq <- seq(from = min(d3$weight), to = max(d3$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)
answer (c) weight 10-30 is underestimated, but <10 and >30 are over-estimated. The assumption may be wrong that I believe the relationship is not be linear.