Module #11 Assignment
#Part 1: Additive Model for the Ashina Dataset > library(ISwR) > data(ashina) > ashina$subject <- factor(1:16) > act <- data.frame(vas = ashina$vas.active, subject = ashina$subject, treat = 1, period = ashina$grp) > plac <- data.frame(vas = ashina$vas.plac, subject = ashina$subject, treat = 0, period = ashina$grp) > combined <- rbind(act, plac) > additive_model <- lm(vas ~ subject + treat + period, data = combined) > summary(additive_model) Call: lm(formula = vas ~ subject + treat + period, data = combined) Residuals: Min 1Q Median 3Q Max -48.94 -18.44 0.00 18.44 48.94 Coefficients: (1 not defined because of singularities) Estimate Std. Error t value Pr(>|t|) (Intercept) -113.06 27.39 -4.128 0.000895 *** subject2 51.50 37.58 1.370 0.190721 subject3 121.50 37.58 3.233 0.005573 ** subject4 97.00 37.58 2.581 0.020867 * subject5 125.00 37.58 3.326 0.004604 ** subject6 31.50 37.58 0.838 0.415070 subject7 119.50 37.58 3.180 0.006215 ** subject8 132.00 37.58 3.513 0.003142 ** subject9 80.50 37.58 2.142 0.049003 * subject10 116.00 37.58 3.087 0.007518 ** subject11 121.50 37.58 3.233 0.005573 ** subject12 154.50 37.58 4.111 0.000925 *** subject13 131.00 37.58 3.486 0.003318 ** subject14 125.00 37.58 3.326 0.004604 ** subject15 99.00 37.58 2.634 0.018768 * subject16 80.50 37.58 2.142 0.049003 * treat -42.87 13.29 -3.227 0.005644 ** period NA NA NA NA --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 37.58 on 15 degrees of freedom Multiple R-squared: 0.7566, Adjusted R-squared: 0.4969 F-statistic: 2.914 on 16 and 15 DF, p-value: 0.02229 > t_test_result <- t.test(ashina$vas.active, ashina$vas.plac, paired = TRUE)#Part 2: Model Matrices and Interactions > a <- gl(2, 2, 8) # Two levels, each repeated twice, for 8 values > b <- gl(2, 4, 8) # Two levels, each repeated four times, for 8 values > x <- 1:8 > y <- c(1:4, 8:5) > z <- rnorm(8) > model_matrix_ab <- model.matrix(~ a * b) # Full interaction model > model_matrix_ab_interaction <- model.matrix(~ a:b) # Only interaction terms > model_matrix_main <- model.matrix(~ a + b) # Main effects only > lm_ab <- lm(z ~ a * b) > lm_ab_interaction <- lm(z ~ a:b) > lm_main <- lm(z ~ a + b) > summary(lm_ab) Call: lm(formula = z ~ a * b) Residuals: 1 2 3 4 5 6 7 8 -0.6626 0.6626 -0.1294 0.1294 -1.0456 1.0456 0.9444 -0.9444 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) -0.0493 0.7812 -0.063 0.953 a2 -0.5231 1.1048 -0.473 0.661 b2 -0.3548 1.1048 -0.321 0.764 a2:b2 1.7508 1.5624 1.121 0.325 Residual standard error: 1.105 on 4 degrees of freedom Multiple R-squared: 0.3224, Adjusted R-squared: -0.1858 F-statistic: 0.6344 on 3 and 4 DF, p-value: 0.6309 > summary(lm_ab_interaction) Call: lm(formula = z ~ a:b) Residuals: 1 2 3 4 5 6 7 8 -0.6626 0.6626 -0.1294 0.1294 -1.0456 1.0456 0.9444 -0.9444 Coefficients: (1 not defined because of singularities) Estimate Std. Error t value Pr(>|t|) (Intercept) 0.8236 0.7812 1.054 0.351 a1:b1 -0.8729 1.1048 -0.790 0.474 a2:b1 -1.3960 1.1048 -1.264 0.275 a1:b2 -1.2277 1.1048 -1.111 0.329 a2:b2 NA NA NA NA Residual standard error: 1.105 on 4 degrees of freedom Multiple R-squared: 0.3224, Adjusted R-squared: -0.1858 F-statistic: 0.6344 on 3 and 4 DF, p-value: 0.6309 > summary(lm_main) Call: lm(formula = z ~ a + b) Residuals: 1 2 3 4 5 6 7 8 -0.2249 1.1003 -0.5671 -0.3083 -1.4833 0.6079 1.3821 -0.5067 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) -0.4870 0.6936 -0.702 0.514 a2 0.3523 0.8009 0.440 0.678 b2 0.5206 0.8009 0.650 0.544 Residual standard error: 1.133 on 5 degrees of freedom Multiple R-squared: 0.1097, Adjusted R-squared: -0.2464 F-statistic: 0.308 on 2 and 5 DF, p-value: 0.7479Interpretation of data:
Full interaction (
z ~ a * b): This includes both main effects and the interaction effect, which lead to singularities ifaandbare perfectly correlated.Only interaction (
z ~ a:b): Excludes main effects, leading to a simpler model, but may miss main effect contributions.Main effects only (
z ~ a + b): Ignores interactions, assumingaandbcontribute independently.
Comments
Post a Comment