x <- c(0.61, 0.93, 0.83, 0.35, 0.54, 0.16, 0.91, 0.62, 0.62)
y <- c(0.67, 0.84, 0.6, 0.18, 0.85, 0.47, 1.1, 0.65, 0.36)
fit <- lm(y~x)
beta <- coef(fit)[[2]]
n <- length(x)
sigma_squared <- sum(fit$residuals^2)/(n-2)
beta1_sigma <- sigma_squared/sum((x-mean(x))^2)
tbeta1 <- beta/sqrt(beta1_sigma) #test whether beta1 is zero
2*pt(tbeta1, n-2,lower.tail = FALSE)
## [1] 0.05296439
sqrt(sigma_squared)
## [1] 0.2229981
library(UsingR)
data(mtcars)
y <- mtcars$mpg
x <- mtcars$wt
fit <- lm(y~x)
n <- length(y)
##Confidence interval for the expected mpg
##does not have the additional 1
sigma <- sqrt(sum(fit$residuals^2)/(n-2))
beta0_sigma <- sqrt(1/n)*sigma
t0025 <- qt(.025, df=n-2)
coef(fit)[[1]] + coef(fit)[[2]]*mean(x) + t0025*beta0_sigma
## [1] 18.99098
newx <- data.frame(x=mean(x))
predict(fit, newdata = newx, interval = ("confidence"))
## fit lwr upr
## 1 20.09062 18.99098 21.19027
Prediction interval for mpg has an additional 1.
newx <- data.frame(x=3)
predict(fit, newdata = newx, interval = ("prediction"))
## fit lwr upr
## 1 21.25171 14.92987 27.57355
fit2 <-lm(y ~I(x/2))
sumCoef <- summary(fit2)$coef
sumCoef[2,1] + qt(0.025,df=n-2)*sumCoef[2,2]
## [1] -12.97262
d <- sum((y-mean(y))^2)
beta0 <- summary(fit)$coef[1]
beta1 <- summary(fit)$coef[2]
ssy <- sum((y-beta1*x-beta0)^2)
ssy/d
## [1] 0.2471672