You are on page 1of 6

HOMEWORK-6

Submitted By: Venkata Sowmya Pillarisetty (vp422)


Vyshnavi Madhavarapu (vm627)
Nanda Kishore Kande (nk493)
Sai Tejaswi Anne (sa2775)
#Q1 Load the ratdrink data frame from the faraway package. See help(ratdrink,
package=‘faraway’) for more explanation of these data. You should see that these data follow
a pattern essentially similar to the dental data and fev1 data used in class. Use
nlme::groupedData to create a groupedData object to exploit method functions in the nlme
package. I show the first few cases of my resulting groupedData object, along with a tell-tale
sign of its class. (5 points)
library(faraway)
library(nlme)

## Warning: package 'nlme' was built under R version 4.1.2

data(ratdrink)

rd.gd<- groupedData(wt ~ weeks | subject,


data=ratdrink,
outer= ~ treat
)
head(rd.gd)

## Grouped Data: wt ~ weeks | subject


## wt weeks subject treat
## 1 57 0 1 control
## 2 86 1 1 control
## 3 114 2 1 control
## 4 139 3 1 control
## 5 172 4 1 control
## 6 60 0 2 control

#Q2 Create a plot showing weight (wt) as a function of time (weeks), subject (subject) and
treatment (treat). The plot should indicate weight trajectories versus time for subjects (rats)
within each of the three treatment groups, essentially similar to plots we’ve seen for the
dental data and fev1 data in class. You may consider the units of weight to be grams. What
does the plot suggest about a statistical model for these data? Be thorough yet concise. (10
points)
plot(rd.gd, outer=~treat, key=FALSE)
We can see from the above plot that the weights have increased linearly throughout the
course time in the control treatment graph. When it comes to thioracil treatment, we see a
lot of irregular growth and the total weight of the rats is low/less than in other models. When
it comes to throxine, there are few parallels between it and the control treatment, and the
growth rate is significantly higher than the other two therapies.
#Q3 Fit a linear longitudinal model with random intercepts and slopes for each subject (rat)
(aka, random coefficients model or linear mixed effects) while allowing each treatment
group to have its own within subject error standard deviation. Your population averaged
(fixed effects) model should allow each treatment group to have its own linear trend in time
(weeks) (own intercept and slope). The model is parameterized for the within subject error
standard deviation of a reference group (the control group here), with the parameters for
the other groups being the ratio of the group standard deviation relative to that of the
reference group. So, a ratio of 1 for a group means that group’s within subject error standard
deviation is no different than that of the reference group, and so on for ratios greater or less
than 1. Compute 95% confidence intervals for these within subject error standard
deviation/ratios (i.e., ‘variance components’), and use these intervals to comment briefly on
whether we need to consider different within subject error variability across treatment
groups. Show your code and output. (10 points)
mod1<- lme(fixed = wt ~ weeks + treat + weeks:treat,
random = ~1+ weeks| subject, weights = varIdent(form = ~1|treat),
data=rd.gd,
method="REML")
summary(mod1)

## Linear mixed-effects model fit by REML


## Data: rd.gd
## AIC BIC logLik
## 899.0619 933.3796 -437.5309
##
## Random effects:
## Formula: ~1 + weeks | subject
## Structure: General positive-definite, Log-Cholesky parametrization
## StdDev Corr
## (Intercept) 5.837838 (Intr)
## weeks 3.787252 -0.15
## Residual 3.513564
##
## Variance function:
## Structure: Different standard deviations per stratum
## Formula: ~1 | treat
## Parameter estimates:
## control thiouracil thyroxine
## 1.000000 1.418156 1.241766
## Fixed effects: wt ~ weeks + treat + weeks:treat
## Value Std.Error DF t-value p-value
## (Intercept) 52.88000 2.036846 105 25.961701 0.0000
## weeks 26.48000 1.248110 105 21.216074 0.0000
## treatthiouracil 4.78000 3.007734 24 1.589236 0.1251
## treatthyroxine -0.79429 3.263284 24 -0.243401 0.8098
## weeks:treatthiouracil -9.37000 1.800108 105 -5.205244 0.0000
## weeks:treatthyroxine 0.66286 1.969457 105 0.336568 0.7371
## Correlation:
## (Intr) weeks trtthr trtthy wks:trtthr
## weeks -0.227
## treatthiouracil -0.677 0.154
## treatthyroxine -0.624 0.142 0.423
## weeks:treatthiouracil 0.158 -0.693 -0.260 -0.098
## weeks:treatthyroxine 0.144 -0.634 -0.098 -0.248 0.439
##
## Standardized Within-Group Residuals:
## Min Q1 Med Q3 Max
## -1.92970033 -0.59904559 0.04322789 0.59292018 1.81160221
##
## Number of Observations: 135
## Number of Groups: 27

intervals(mod1, which = "var-cov" )

## Approximate 95% confidence intervals


##
## Random Effects:
## Level: subject
## lower est. upper
## sd((Intercept)) 4.0324373 5.8378382 8.4515523
## sd(weeks) 2.7557572 3.7872522 5.2048413
## cor((Intercept),weeks) -0.5571028 -0.1497601 0.3156714
##
## Variance function:
## lower est. upper
## thiouracil 0.9952585 1.418156 2.020748
## thyroxine 0.8409840 1.241766 1.833545
##
## Within-group standard error:
## lower est. upper
## 2.717199 3.513564 4.543330

Yes, different within-subject error variability across treatment groups must be considered.
Because there is no "1" inside the 95% confidence ranges for variance components. As we
can see, the interval is between 2.717199 and 4.54330, and it does not include 1, thus we
must take it into account.
#Q4 Fit a reduced model having common within subject error (uncorrelated) variance and
conduct a likelihood ratio test of equal within subject variability across groups verses
different variability across groups. Report your code, output and conclusion, and comment
on how this test agrees or disagrees with you assessment based on the intervals in the
previous item. (10 points)
mod2<- update(mod1,

weights = varIdent(form = ~ 1), ## Constant Ei (essentially, ni...)


)

summary(mod2)

## Linear mixed-effects model fit by REML


## Data: rd.gd
## AIC BIC logLik
## 898.6754 927.2735 -439.3377
##
## Random effects:
## Formula: ~1 + weeks | subject
## Structure: General positive-definite, Log-Cholesky parametrization
## StdDev Corr
## (Intercept) 5.700452 (Intr)
## weeks 3.760179 -0.133
## Residual 4.347959
##
## Fixed effects: wt ~ weeks + treat + weeks:treat
## Value Std.Error DF t-value p-value
## (Intercept) 52.88000 2.093753 105 25.256087 0.0000
## weeks 26.48000 1.266073 105 20.915058 0.0000
## treatthiouracil 4.78000 2.961013 24 1.614312 0.1195
## treatthyroxine -0.79429 3.262878 24 -0.243431 0.8097
## weeks:treatthiouracil -9.37000 1.790498 105 -5.233180 0.0000
## weeks:treatthyroxine 0.66286 1.973033 105 0.335958 0.7376
## Correlation:
## (Intr) weeks trtthr trtthy wks:trtthr
## weeks -0.250
## treatthiouracil -0.707 0.177
## treatthyroxine -0.642 0.160 0.454
## weeks:treatthiouracil 0.177 -0.707 -0.250 -0.113
## weeks:treatthyroxine 0.160 -0.642 -0.113 -0.250 0.454
##
## Standardized Within-Group Residuals:
## Min Q1 Med Q3 Max
## -1.83136767 -0.54991159 0.04002717 0.58231430 2.03660036
##
## Number of Observations: 135
## Number of Groups: 27

anova(mod2 , mod1 )

## Model df AIC BIC logLik Test L.Ratio p-value


## mod2 1 10 898.6754 927.2735 -439.3377
## mod1 2 12 899.0619 933.3796 -437.5309 1 vs 2 3.613531 0.1642

We can see that the P value is high (> 0.5), hence model 2 (the simplified model) is the best
model. We can see from the preceding question that we need to account for subject error
variability between treatments, thus we go with the reduced model, which is model 2.

#Q5 Regardless of your conclusions, above, continue here with the smaller of the above two
models, the one with constant variance of errors within subjects. Report a partial anova table
(i.e., ‘type 3’ (car::Anova) or ‘marginal’ (nlme::anova.lme), as we’ve presented in our notes,
and comment on the (population averaged, fixed) effects of treatment on weight. Be sure to
consider any interaction of the treatments with weeks (i.e., don’t violate the marginality
principle.) You may find that a summary of the fixed effects will help you to explain such
effects. Discuss your results in the context of the original plot, above. (10 points)
car::Anova(mod2, type=3)

## Analysis of Deviance Table (Type III tests)


##
## Response: wt
## Chisq Df Pr(>Chisq)
## (Intercept) 637.8699 1 < 2.2e-16 ***
## weeks 437.4397 1 < 2.2e-16 ***
## treat 3.8053 2 0.1492
## weeks:treat 36.6375 2 1.107e-08 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

As we can see from the varied P values, the p value for the treatments is high, while the others
are notably low, therefore we can conclude that the treatments contribute the most. The
graphs also support the same conclusion.

You might also like