Module 11

 


Module 11 Assignment 


**Question 10.1: Set up an additive model for the ashina data**

To set up an additive model for the ashina data, you can follow these steps. The data contains additive effects on subjects, period, and treatment. We'll use linear models to compare the results with t-tests.


```R

# Load the ISwR package

library(ISwR)


# Load the ashina data

data(ashina)


# Create a subject factor

ashina$subject <- factor(1:16)


# Attach the dataset for easier access

attach(ashina)


# Create data frames for active and placebo treatments

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

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


# Fit linear models for active and placebo treatments

model_act <- lm(vas ~ subject + period + treat, data = act)

model_plac <- lm(vas ~ subject + period + treat, data = plac)


# Compare the results with t-tests

t_test_act <- t.test(vas.active ~ treat)

t_test_plac <- t.test(vas.plac ~ treat)


# View the model summaries and t-test results

summary(model_act)

summary(model_plac)

t_test_act

t_test_plac

```

In this code, we load the ISwR package, load the ashina data, create subject factors, and create data frames for active and placebo treatments. We fit linear models for active and placebo treatments, and also perform t-tests to compare the results.


**Question 10.3: Generate the model matrices for models z ~ a*b, z ~ a:b, etc.**


To generate the model matrices for models like z ~ a*b and z ~ a:b, you can use the following code:


```R

# Define variables

a <- gl(2, 2, 8)

b <- gl(2, 4, 8)

x <- 1:8

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

z <- rnorm(8)


# Generate model matrices for various models

model_matrix_a_b <- model.matrix(~ a * b)

model_matrix_a_colon_b <- model.matrix(~ a:b)


# Print the model matrices

model_matrix_a_b

model_matrix_a_colon_b


# Fit linear models

lm_a_b <- lm(z ~ model_matrix_a_b)

lm_a_colon_b <- lm(z ~ model_matrix_a_colon_b)


# Print the model summaries

summary(lm_a_b)

summary(lm_a_colon_b)

```

In this code, we define the variables a, b, x, y, and z. We then generate model matrices for models z ~ a * b and z ~ a:b. Finally, we fit linear models for these models and print the model summaries.


The main difference between these models is how they represent the interaction between the variables a and b. The `a * b` model includes both main effects and the interaction effect, while the `a:b` model represents only the interaction effect. The choice between these models depends on the research question and the nature of the relationship between the variables. The models containing singularities may indicate the presence of high correlations between two or more independent variables. Researchers need to carefully interpret the results and consider the implications for their specific analysis.

Comments