Module 11. Assignment

Question 1: 


> 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_data <- rbind(act, plac)
> model <- lm(vas ~ subject + treat + period, data = combined_data)
> summary(model)

Call:
lm(formula = vas ~ subject + treat + period, data = combined_data)

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(ashina$vas.active, ashina$vas.plac, paired = TRUE)

	Paired t-test

data:  ashina$vas.active and ashina$vas.plac
t = -3.2269, df = 15, p-value = 0.005644
alternative hypothesis: true mean difference is not equal to 0
95 percent confidence interval:
 -71.1946 -14.5554
sample estimates:
mean difference 
        -42.875 


The treatment has a value of -42.87, stating that the active treatment reduces 
pain scores. The people that go the active treatment 43% less pain than those who 
got placebo. The p-value of treatment is 0.0056, which is less than 0.05, so it 
is significant.

Each subject has an individual pain score, and there is variation as it goes from
31 all the way to 154. There was no impact on different periods. 

The t-results' p-value is 0.005644 which is less than 0.05. It is significant
that the active treatment reduces pain compared to the placebo. The confidence
interval is from -71.19 to -14.56 so the pain reduction from the active
treatment is between 14.56 and 71.19


Question 2:


> a <- gl(2, 2, 8)  
> b <- gl(2, 4, 8)  
> x <- -1:8         
> y <- c(1:4, 8:5)  
> z <- rnorm(8)
> interaction_model_matrix <- model.matrix(z ~ a*b)
> interaction_model_matrix
  (Intercept) a2 b2 a2:b2
1           1  0  0     0
2           1  0  0     0
3           1  1  0     0
4           1  1  0     0
5           1  0  1     0
6           1  0  1     0
7           1  1  1     1
8           1  1  1     1
attr(,"assign")
[1] 0 1 2 3
attr(,"contrasts")
attr(,"contrasts")$a
[1] "contr.treatment"

attr(,"contrasts")$b
[1] "contr.treatment"

> interaction_model <- lm (z ~ a*b)
> summary(interaction_model)

Call:
lm(formula = z ~ a * b)

Residuals:
       1        2        3        4        5        6        7        8 
-0.32576  0.32576 -0.03928  0.03928 -0.08263  0.08263 -0.11748  0.11748 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)   0.6907     0.1791   3.857 0.018201 *  
a2           -2.1479     0.2533  -8.481 0.001060 ** 
b2           -1.0594     0.2533  -4.183 0.013887 *  
a2:b2         3.4278     0.3582   9.570 0.000666 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.2533 on 4 degrees of freedom
Multiple R-squared:  0.9652,	Adjusted R-squared:  0.939 
F-statistic: 36.94 on 3 and 4 DF,  p-value: 0.002249



> only_interaction_model_matrix <- model.matrix(~ a:b)
> only_interaction_model_matrix
  (Intercept) a1:b1 a2:b1 a1:b2 a2:b2
1           1     1     0     0     0
2           1     1     0     0     0
3           1     0     1     0     0
4           1     0     1     0     0
5           1     0     0     1     0
6           1     0     0     1     0
7           1     0     0     0     1
8           1     0     0     0     1
attr(,"assign")
[1] 0 1 1 1 1
attr(,"contrasts")
attr(,"contrasts")$a
[1] "contr.treatment"

attr(,"contrasts")$b
[1] "contr.treatment"

> only_interaction_model <- lm (z ~ a:b)
> summary(only_interaction_model)

Call:
lm(formula = z ~ a:b)

Residuals:
       1        2        3        4        5        6        7        8 
-0.32576  0.32576 -0.03928  0.03928 -0.08263  0.08263 -0.11748  0.11748 

Coefficients: (1 not defined because of singularities)
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)   0.9111     0.1791   5.088 0.007043 ** 
a1:b1        -0.2205     0.2533  -0.871 0.433121    
a2:b1        -2.3684     0.2533  -9.351 0.000728 ***
a1:b2        -1.2799     0.2533  -5.053 0.007214 ** 
a2:b2             NA         NA      NA       NA    
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.2533 on 4 degrees of freedom
Multiple R-squared:  0.9652,	Adjusted R-squared:  0.939 
F-statistic: 36.94 on 3 and 4 DF,  p-value: 0.002249





The model with (z ~ a*b) includes the effects of a and b and the interaction 
between them a:b. While the formula (z ~ a:b) only includes the interaction between
a and b, without the main effects of a or b. (z ~ a*b) is more flexible as it
includes both types of effects. (z ~ a:b) is more likely to involve singularities
because it depends on one interaction.













Comments