STA108HW4-1
STA108HW4-1
STA108HW4-1
Nitin Kanchi
2023-11-08
Problem - 1
a
[,1]
[1,] 819499
[,1] [,2]
[1,] 16 448
[2,] 448 13824
[,1]
[1,] 3609
[2,] 103656
[,1] [,2]
[1,] 0.675000 -0.02187500
[2,] -0.021875 0.00078125
[,1]
[1,] 146.425
[,1] [,2]
[1,] 1 30
[1] 0.6863672
1
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 0.825 -0.175 -0.175 -0.175 -0.100 -0.100 -0.100 -0.100 -0.025 -0.025
[2,] -0.175 0.825 -0.175 -0.175 -0.100 -0.100 -0.100 -0.100 -0.025 -0.025
[3,] -0.175 -0.175 0.825 -0.175 -0.100 -0.100 -0.100 -0.100 -0.025 -0.025
[4,] -0.175 -0.175 -0.175 0.825 -0.100 -0.100 -0.100 -0.100 -0.025 -0.025
[5,] -0.100 -0.100 -0.100 -0.100 0.925 -0.075 -0.075 -0.075 -0.050 -0.050
[6,] -0.100 -0.100 -0.100 -0.100 -0.075 0.925 -0.075 -0.075 -0.050 -0.050
[7,] -0.100 -0.100 -0.100 -0.100 -0.075 -0.075 0.925 -0.075 -0.050 -0.050
[8,] -0.100 -0.100 -0.100 -0.100 -0.075 -0.075 -0.075 0.925 -0.050 -0.050
[9,] -0.025 -0.025 -0.025 -0.025 -0.050 -0.050 -0.050 -0.050 0.925 -0.075
[10,] -0.025 -0.025 -0.025 -0.025 -0.050 -0.050 -0.050 -0.050 -0.075 0.925
[11,] -0.025 -0.025 -0.025 -0.025 -0.050 -0.050 -0.050 -0.050 -0.075 -0.075
[12,] -0.025 -0.025 -0.025 -0.025 -0.050 -0.050 -0.050 -0.050 -0.075 -0.075
[13,] 0.050 0.050 0.050 0.050 -0.025 -0.025 -0.025 -0.025 -0.100 -0.100
[14,] 0.050 0.050 0.050 0.050 -0.025 -0.025 -0.025 -0.025 -0.100 -0.100
[15,] 0.050 0.050 0.050 0.050 -0.025 -0.025 -0.025 -0.025 -0.100 -0.100
[16,] 0.050 0.050 0.050 0.050 -0.025 -0.025 -0.025 -0.025 -0.100 -0.100
[,11] [,12] [,13] [,14] [,15] [,16]
[1,] -0.025 -0.025 0.050 0.050 0.050 0.050
[2,] -0.025 -0.025 0.050 0.050 0.050 0.050
[3,] -0.025 -0.025 0.050 0.050 0.050 0.050
[4,] -0.025 -0.025 0.050 0.050 0.050 0.050
[5,] -0.050 -0.050 -0.025 -0.025 -0.025 -0.025
[6,] -0.050 -0.050 -0.025 -0.025 -0.025 -0.025
[7,] -0.050 -0.050 -0.025 -0.025 -0.025 -0.025
[8,] -0.050 -0.050 -0.025 -0.025 -0.025 -0.025
[9,] -0.075 -0.075 -0.100 -0.100 -0.100 -0.100
[10,] -0.075 -0.075 -0.100 -0.100 -0.100 -0.100
[11,] 0.925 -0.075 -0.100 -0.100 -0.100 -0.100
[12,] -0.075 0.925 -0.100 -0.100 -0.100 -0.100
[13,] -0.100 -0.100 0.825 -0.175 -0.175 -0.175
[14,] -0.100 -0.100 -0.175 0.825 -0.175 -0.175
[15,] -0.100 -0.100 -0.175 -0.175 0.825 -0.175
[16,] -0.100 -0.100 -0.175 -0.175 -0.175 0.825
sx rk yr dg yd sl
1 male full 25 doctorate 35 36350
2 male full 13 doctorate 22 35350
3 male full 10 doctorate 23 28200
4 female full 7 doctorate 27 26775
5 male full 19 masters 30 33696
6 male full 16 doctorate 21 28516
Call:
lm(formula = sl ~ yd + dg, data = salary)
Residuals:
Min 1Q Median 3Q Max
-8589.8 -2724.4 -682.9 2391.1 9486.0
2
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 17422.01 1066.60 16.334 < 0.0000000000000002 ***
yd 483.99 63.98 7.565 0.000000000889 ***
dgmasters -4113.89 1361.44 -3.022 0.00399 **
---
Signif. codes: 0 ’***’ 0.001 ’**’ 0.01 ’*’ 0.05 ’.’ 0.1 ’ ’ 1
1
21293.93
A person who has 8 years of experience and holds an doctorate the salary this person should have is estimated
to be $21293.93 according to the model.
e
2.5 % 97.5 %
yd 355.42 612.5604
we are 95% confident that b1 is within the interval (355.42, 612.56). This implies that for every year of
experience that increases we can say that the salary increases from $355.42 to 612.56 dollars.
problem 2
a
1.25 % 98.75 %
(Intercept) 14955.6292 19888.3819
yd 336.0472 631.9332
dgmasters -7262.0450 -965.7403
b the coefficient for dgmasters is -4113.89, which means subjects with a master’s degree earn, on average,
$4113.89 less over three months than subjects with a doctorate, holding years of experience constant.
c
3
Scheffe-adjusted prediction intervals
The difference is between the fitted values $2661.93 is the smallest expected difference in salary between
subject 1 and subject 2.
XTX_inv
SSE
s2_b
Xh <- matrix(c(1, 30), nrow = 1) # Converts Xh into a 1x2 matrix (row vector with an intercept)
s2_pred <- as.numeric(Xh %*% XTX_inv %*% t(Xh)) * as.numeric(SSE / (nrow(X) - ncol(X)))
Xh
s2_pred
M <- diag(rep(1, nrow(X))) - H
M
salary<-read.csv('salary2.csv')
head(salary)
salary$dg <- as.factor(salary$dg)
model<- lm(data = salary, sl ~ yd + dg)
summary(model)
4
newdata <- data.frame(yd = 8, dg = factor("doctorate", levels = levels(salary$dg)))
predicted_salary <- predict(model, newdata)
print(predicted_salary)
confint(model, "yd", level = 0.95)
confint_bonferroni <- confint(model, level = 1 - (0.05/2))
confint_bonferroni
newdata<-data.frame(yd = c(4, 8), dg = factor(c("masters", "doctorate"), levels = levels(salary$dg)))
predict_adjust(model, newdata = newdata,interval = "prediction",method = "scheffe",
level=0.90)
new_data2 <- data.frame(yd = c(5, 2), dg = factor(c("masters", "doctorate"), levels = levels(salary$dg))
predictions2 <- predict(model, new_data2, interval = "prediction", level = 0.95)
predictions2