STA108HW4-1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

STAHW4

Nitin Kanchi

2023-11-08

Warning: package ’readr’ was built under R version 4.0.5

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] 7.059776786 0.008171038

[,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

in the R section # Problem - 1

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

Residual standard error: 4090 on 49 degrees of freedom


Multiple R-squared: 0.541, Adjusted R-squared: 0.5222
F-statistic: 28.87 on 2 and 49 DF, p-value: 0.00000000519

Y = 17422.01 + 483.99X1 - 4113.89X2(X2 = 1 if it’s masters)


b b1 represents the additional salary associated with each additional year of experience after the highest
degree was earned, assuming the degree type is held constant
c b2 represents the difference in salary between a doctorate and a master’s degree holder, assuming the
number of years since the highest degree type is held constant.
d

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

Family-wise confidence level of at least 0.9

fit lwr upr


1 15244.07 5638.187 24849.96
2 21293.93 12152.719 30435.14

fit lwr upr


1 15728.06 6977.768 24478.36
2 18389.99 9940.885 26839.09

The difference is between the fitted values $2661.93 is the smallest expected difference in salary between
subject 1 and subject 2.

knitr::opts_chunk$set(echo = FALSE, comment = NA, message = FALSE)


options(scipen = 999) #Remove the scientific notation
library(readr)
library(api2lm)
plastic<-read_tsv('Plastic-hardness.txt')
Y <- as.matrix(plastic$Y)
X <- as.matrix(cbind(1, plastic$X)) # Adding a column of 1s for the intercept
YTY <- t(Y) %*% Y
XTX <- t(X) %*% X
XTY <- t(X) %*% Y
print(YTY)
print(XTX)
print(XTY)
XTX_inv <- solve(XTX)
b <- XTX_inv %*% XTY
Y_hat <- X %*% b
H <- X %*% XTX_inv %*% t(X)
SSE <- t(Y - Y_hat) %*% (Y - Y_hat)
s2_b <- diag(XTX_inv * as.numeric(SSE / (nrow(X) - ncol(X))))

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy