## 'data.frame': 30 obs. of 5 variables:
## $ Case : int 1 2 3 4 5 6 7 8 9 10 ...
## $ taste : num 12.3 20.9 39 47.9 5.6 25.9 37.3 21.9 18.1 21 ...
## $ Acetic: num 4.54 5.16 5.37 5.76 4.66 ...
## $ H2S : num 3.13 5.04 5.44 7.5 3.81 ...
## $ Lactic: num 0.86 1.53 1.57 1.81 0.99 1.09 1.29 1.78 1.29 1.58 ...
m1 <- lm(taste ~ Acetic + H2S + Lactic, dat) # regression model
m0 <- lm(taste ~ 1, dat) # baseline model
## Parameter estimation
coef(m1) # OLS estimates## (Intercept) Acetic H2S Lactic
## -28.8767696 0.3277413 3.9118411 19.6705434
## (Intercept) Acetic H2S Lactic
## (Intercept) 389.486737 -77.979215 8.960302 -7.332453
## Acetic -77.979215 19.889429 -2.089205 -13.148377
## H2S 8.960302 -2.089205 1.558578 -4.670244
## Lactic -7.332453 -13.148377 -4.670244 74.460587
## 2.5 % 97.5 %
## (Intercept) -69.443503 11.689964
## Acetic -8.839420 9.494902
## H2S 1.345656 6.478026
## Lactic 1.933267 37.407820
OLS estimation (Shiny app): https://apps.mathpsy.uni-tuebingen.de/fw/ols/
## Point and interval prediction
predict(m1,
newdata = data.frame(Acetic = 3:7,
H2S = mean(dat$H2S),
Lactic = mean(dat$Lactic)),
interval = "prediction")## fit lwr upr
## 1 23.71462 -7.470247 54.89950
## 2 24.04237 -1.190142 49.27487
## 3 24.37011 2.715170 46.02504
## 4 24.69785 3.035281 46.36042
## 5 25.02559 -0.226560 50.27774
##
## Call:
## lm(formula = taste ~ Acetic + H2S + Lactic, data = dat)
##
## Residuals:
## Min 1Q Median 3Q Max
## -17.390 -6.612 -1.009 4.908 25.449
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -28.8768 19.7354 -1.463 0.15540
## Acetic 0.3277 4.4598 0.073 0.94198
## H2S 3.9118 1.2484 3.133 0.00425 **
## Lactic 19.6705 8.6291 2.280 0.03108 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 10.13 on 26 degrees of freedom
## Multiple R-squared: 0.6518, Adjusted R-squared: 0.6116
## F-statistic: 16.22 on 3 and 26 DF, p-value: 3.81e-06
## Analysis of Variance Table
##
## Model 1: taste ~ 1
## Model 2: taste ~ Acetic + H2S + Lactic
## Res.Df RSS Df Sum of Sq F Pr(>F)
## 1 29 7662.9
## 2 26 2668.4 3 4994.5 16.221 3.81e-06 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Linear hypothesis \(\beta_1 = 0\) and \(\beta_2 = \beta_3\)
X <- model.matrix(m1)
C <- matrix(c(0,1,0,0, 0,0,1,-1), 2, 4, byrow = TRUE)
diff <- C %*% coef(m1) - c(0, 0)
D <- solve(C %*% solve(t(X) %*% X) %*% t(C))
Q <- t(diff) %*% D %*% diff
r <- qr(C)$rank
(F <- Q/r / sigma(m1)^2)## [,1]
## [1,] 1.606742
## [1] 3.369016
##
## Linear hypothesis test:
## Acetic = 0
## H2S - Lactic = 0
##
## Model 1: restricted model
## Model 2: taste ~ Acetic + H2S + Lactic
##
## Res.Df RSS Df Sum of Sq F Pr(>F)
## 1 28 2998.2
## 2 26 2668.4 2 329.8 1.6067 0.2198
## Analysis of Variance Table
##
## Model 1: taste ~ I(H2S + Lactic)
## Model 2: taste ~ Acetic + H2S + Lactic
## Res.Df RSS Df Sum of Sq F Pr(>F)
## 1 28 2998.2
## 2 26 2668.4 2 329.8 1.6067 0.2198
## Q-Q plot
par(mfrow = c(1, 2)) # split plotting window
qqnorm(resid(m1), main = "Observed")
qqline(resid(m1))
qqnorm(y <- rnorm(30), main = "Simulated")
qqline(y)## Residuals vs. fitted values
par(mfrow = c(1, 1))
plot(resid(m1) ~ fitted(m1),
main = "", xlab = "Predicted values", ylab = "Residuals")
abline(h = 0, lty = 2)## 1 2 3 4 5 6 7
## 0.17525784 0.07593130 0.05994339 0.08829409 0.12879533 0.23036705 0.20709897
## 8 9 10 11 12 13 14
## 0.08333780 0.08291114 0.12013909 0.06531941 0.14929496 0.14821335 0.04332811
## 15 16 17 18 19 20 21
## 0.09000337 0.15153827 0.08934443 0.06198950 0.08249992 0.26029095 0.14521419
## 22 23 24 25 26 27 28
## 0.03912430 0.20545696 0.23343680 0.08406925 0.26606306 0.14973461 0.07036401
## 29 30
## 0.19818511 0.21445340
H <- X %*% solve(t(X) %*% X) %*% t(X) # hat matrix
h <- diag(H)
## Cook's distance
cooks.distance(m1)## 1 2 3 4 5 6
## 0.0692954594 0.0006538367 0.0322134155 0.0256812534 0.0008307915 0.0028512031
## 7 8 9 10 11 12
## 0.0179979270 0.0730650866 0.0058503793 0.0002133561 0.0042356476 0.1231598979
## 13 14 15 16 17 18
## 0.0099034331 0.0076724201 0.1714653442 0.0083806741 0.0067909340 0.0345647920
## 19 20 21 22 23 24
## 0.0291892000 0.0858382548 0.0001742073 0.0018567690 0.0230305489 0.0172589455
## 25 26 27 28 29 30
## 0.0035559153 0.0095340749 0.0307525936 0.0288491545 0.0370008615 0.1017165104
## 1 2 3 4 5 6
## 0.0692954594 0.0006538367 0.0322134155 0.0256812534 0.0008307915 0.0028512031
## 7 8 9 10 11 12
## 0.0179979270 0.0730650866 0.0058503793 0.0002133561 0.0042356476 0.1231598979
## 13 14 15 16 17 18
## 0.0099034331 0.0076724201 0.1714653442 0.0083806741 0.0067909340 0.0345647920
## 19 20 21 22 23 24
## 0.0291892000 0.0858382548 0.0001742073 0.0018567690 0.0230305489 0.0172589455
## 25 26 27 28 29 30
## 0.0035559153 0.0095340749 0.0307525936 0.0288491545 0.0370008615 0.1017165104
## taste Acetic H2S Lactic
## taste 1.0000000 0.5495393 0.7557523 0.7042362
## Acetic 0.5495393 1.0000000 0.6179559 0.6037826
## H2S 0.7557523 0.6179559 1.0000000 0.6448123
## Lactic 0.7042362 0.6037826 0.6448123 1.0000000
## Variance inflation factor
c(Acetic = 1/(1 - summary(lm(Acetic ~ H2S + Lactic, dat))$r.squared),
H2S = 1/(1 - summary(lm(H2S ~ Acetic + Lactic, dat))$r.squared),
Lactic = 1/(1 - summary(lm(Lactic ~ Acetic + H2S, dat))$r.squared))## Acetic H2S Lactic
## 1.831589 1.992200 1.937912
## Adjusted coefficient of determination
n <- nrow(dat)
p <- 3
1 - (n - 1)/(n - p - 1) * (1 - summary(m1)$r.squared)## [1] 0.6115948
## [1] 0.6115948
Generalizability (Shiny app): https://apps.mathpsy.uni-tuebingen.de/fw/generalizability/
n <- 30 # adjust sample size until target power is reached
dat <- expand.grid(x1 = rep(c(0, 2, 4, 6, 8), each = n/(5*3)),
x2 = c(30, 50, 80))
pval <- replicate(2000, {
y <- 35 - 2.5*dat$x1 + 0.1*dat$x2 + rnorm(n, sd = 14)
m0 <- lm(y ~ 1, dat)
m1 <- lm(y ~ x1 + x2, dat)
anova(m0, m1)$"Pr(>F)"[2] # test H0
})
mean(pval < 0.05) # power estimate## [1] 0.6655
More power-simulation examples: https://doi.org/10.48550/arXiv.2110.09836