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(μ, σ) 

4E2. In the model definition just above, how many parameters are in the posterior distribution?

#Two:μ and σ.

4E3. Using the model definition above, write down the appropriate form of Bayes’ theorem that includes the proper likelihood and priors.

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

#μi=α+βxi

4E5. In the model definition just above, how many parameters are in the posterior distribution?

#Three: α , β, 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}\]

library(car)
## Loading required package: carData
sample_mu <- rnorm(1e4, 0, 10)
sample_sigma <- runif(1e4, 0, 10)
prior_y <- rnorm(1e4, sample_mu, sample_sigma)
densityPlot(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 )

#yi∼Normal(μ,σ)
#μi=α+β
#α∼Normal(0,50)
#β∼Uniform(0,10)
#σ∼Uniform(0,50)

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.

#hi∼Normal(μ,σ)
#μi=α+βxi
#α∼Normal(150,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?

#hi∼Normal(μ,σ)
#μi=α+βxi
#α∼Normal(120,10)
#β∼Normal(7,1)
#σ∼Uniform(0,20)

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?

#hi∼Normal(μ,σ)
#μi=α+βxi
#α∼Normal(120,10)
#β∼Normal(7,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.

Howell1 <- read.csv("C:/Users/PC/Downloads/Howell1.csv", sep=";")
data("Howell1")
## Warning in data("Howell1"): data set 'Howell1' not found
data_howel<-Howell1[Howell1$age >=18,]
xbar<-mean(data_howel$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=data_howel)
#m4.3

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?

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.

  1. 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?

  2. 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.

  3. 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
library(purrr)
## 
## Attaching package: 'purrr'
## The following object is masked from 'package:car':
## 
##     some
# Part 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)

# Part b

#plot(height ~ weight, data = d2, col = col.alpha(rangi2, 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)