Module # 11 assignment

 Issaiah Jennings 

Module # 11 assignment


> # Load the necessary package and data

> library(ISwR)

> data(ashina)

> # Set up factors and prepare data

> ashina$subject <- factor(1:16)

> attach(ashina)

> act <- data.frame(vas = vas.active, subject = subject, treat = 1, period = grp)

> plac <- data.frame(vas = vas.plac, subject = subject, treat = 0, period = grp)

> combined_data <- rbind(act, plac)

> # Fit the additive model and view summary

> additive_model <- lm(vas ~ subject + treat + period, data = combined_data)

> summary(additive_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


> # Perform paired t-test for comparison

> t.test(vas.active, vas.plac, paired = TRUE)


Paired t-test


data:  vas.active and 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 


> # Define factors a and b

> a <- gl(2, 2, 8)  # Creates factor levels (1, 1, 2, 2) repeated

> b <- gl(2, 4, 8)  # Creates factor levels (1, 1, 1, 1, 2, 2, 2, 2)

> # Define other variables

> x <- 1:8

> y <- c(1:4, 8:5)

> z <- rnorm(8)  

> # Generate model matrices for interaction terms

> matrix_a_b <- model.matrix(~ a * b)       

> matrix_a_colon_b <- model.matrix(~ a:b)   

> # Fit linear models with interaction terms

> model_a_b <- lm(z ~ a * b)         

> model_a_colon_b <- lm(z ~ a:b)      

> # Display summaries of the models

> summary(model_a_b)


Call:

lm(formula = z ~ a * b)


Residuals:

       1        2        3        4        5        6        7        8 

 0.54124 -0.54124  0.22015 -0.22015 -0.28026  0.28026  0.01003 -0.01003 


Coefficients:

            Estimate Std. Error t value Pr(>|t|)  

(Intercept)  -0.8493     0.3241  -2.621   0.0587.

a2            0.9411     0.4583   2.054   0.1093  

b2            1.6872     0.4583   3.682   0.0212 *

a2:b2        -1.2399     0.6481  -1.913   0.1283  

---

Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1


Residual standard error: 0.4583 on 4 degrees of freedom

Multiple R-squared:  0.7948, Adjusted R-squared:  0.6408 

F-statistic: 5.163 on 3 and 4 DF,  p-value: 0.07335


> summary(model_a_colon_b)


Call:

lm(formula = z ~ a:b)


Residuals:

       1        2        3        4        5        6        7        8 

 0.54124 -0.54124  0.22015 -0.22015 -0.28026  0.28026  0.01003 -0.01003 


Coefficients: (1 not defined because of singularities)

            Estimate Std. Error t value Pr(>|t|)  

(Intercept)   0.5391     0.3241   1.664   0.1715  

a1:b1        -1.3884     0.4583  -3.030   0.0388 *

a2:b1        -0.4473     0.4583  -0.976   0.3843  

a1:b2         0.2988     0.4583   0.652   0.5500  

a2:b2             NA         NA      NA       NA  

---

Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1


Residual standard error: 0.4583 on 4 degrees of freedom

Multiple R-squared:  0.7948, Adjusted R-squared:  0.6408 

F-statistic: 5.163 on 3 and 4 DF,  p-value: 0.07335





Model Summary


- Min/Max Residuals: Lowest and highest values of residuals, reflecting model fit accuracy.

- 1Q/3Q: First and third quartiles of residuals, marking lower and upper ranges.

- Median: Middle residual value.

- Estimate: Coefficient for each predictor, showing its influence on the outcome.

- Std. Error: Variability in each coefficient’s estimate.

- t Value: Tests if each coefficient significantly differs from zero.

- Pr(>|t|): P-value for the t-test, indicating statistical significance.

- Residual Standard Error**: Average distance of observed values from the regression line.

- Multiple R-squared: Proportion of outcome variance explained by the model.

- Adjusted R-squared: R-squared adjusted for predictor count and sample size.

- F-statistic and p-value: Assess the model’s overall statistical significance.


---


Comparing Models: `z ~ a * b` and `z ~ a:b`

- `z ~ a * b`: Includes main effects of `a` and `b` and their interaction, showing both individual and combined effects on `z`.

  - Equation: `z = Intercept + (Effect of a) + (Effect of b) + (a:b Interaction) + Error`


- `z ~ a:b`: Only the interaction term (`a:b`), excluding individual effects, meaning `z` is modeled solely on the joint influence of `a` and `b`.

  - Equation: `z = Intercept + (a:b Interaction) + Error`


Comparison of `t.test()` vs `lm()`

- `t.test()`: Ideal for simple mean difference testing between two groups; provides a t-value and p-value.

- `lm()`: Versatile, enabling analysis of multiple predictors and interactions. It’s useful for examining complex models beyond simple mean differences, giving insights into each predictor's effect and interactions on the outcome.



Comments

Popular posts from this blog

The Final project in this class

Module # 8 Assignment

Module # 6 assignment