1.20
First, import the data.
setwd("C:/Users/Dongrui/Desktop/21 Fall/Linear Regression Models/HW/HW1")
data = read.table("CH01PR20.txt",header = FALSE)
Y = data[, 1]
X = data[, 2]
Part (a)
b1 = cov(X, Y)/var(X)
b0 = mean(Y) - b1*mean(X)
\(b_1 = 15.035248\), \(b_0 = -0.5801567\).
The estimated regression function is \[ \hat{y} =-0.5801567 + 15.035248x. \] Part (b)
plot(X, Y)
abline(b0, b1)
It fits reasonably well.
Part (c)
b0 in this case shows that when the number of the copier machine serviced is zero, the total number of minutes spent by the service person is \(-0.5801567\), which doesnโt have any information.
Part (d)
m=(b1*5+b0)/5
The point estimate of the mean service time when X = \(14.9192167\).
1.42
First, import the data and write a function that output the likelihood given the data and \(\beta_1\).
x = c(7, 12, 4, 14, 25, 30)
y = c(128, 213, 75, 250, 446, 540)
Likelihood = function(x, y, beta1) {
return(prod(1/(sqrt(2*pi)*4)*exp(-(y- beta1*x)^2/32 )))
}
Part (a) \[ \frac{1}{{(32\pi)}^3}\exp \bigg\{ - \frac{\sum_{i = 1}^{6} (Y_i - \beta_1 X_i)^2}{32} \bigg\} \] Part (b)
For _1 = 17, Likelihood(x,y,17) = 9.4513295^{-30}
For _1 = 18, Likelihood(x,y,18) = 2.6490426^{-7}
For _1 = 19, Likelihood(x,y,19) = 3.0472851^{-37}
Part (c)
Estimator = function(x, y) {
return(sum(x*y)/sum(x^2))
}
Using the maximum likelihood estimator we have b1 = 17.9284974, which is close to 18 and therefore consistent with results in (b)
Part (d)
library(ggplot2)
Likelihood = function(beta1) {
x = c(7, 12, 4, 14, 25, 30)
y = c(128, 213, 75, 250, 446, 540)
return(prod(1/(sqrt(2*pi)*4)*exp(-(y- rep(beta1, each = length(x), length.out=length(x))*x)^2/32 )))
}
beta1<-seq(17, 19, by=0.01)
y1<-lapply(beta1,Likelihood)
y1<-unlist(y1)
df<-data.frame(beta1, y1)
g <- ggplot(df, aes(beta1,y1))
g <- g + geom_line(col='red')
g <- g + geom_hline(yintercept = 0) + geom_vline(xintercept = Estimator(x,y))
g <- g + ggtitle("Likelihood")
g
Yes,the point at which the likelihood function is maximized(17.9284974) correspond to the maximum likelihood estimate found in part (c).