R-help to Exercise 11 i BSS
#
QUESTION a)
#
Read the data and variable names into a data frame, and take
a look at the data
aserum<-read.table("http://www.uio.no/studier/emner/matnat/math/STK4900/v07/annet/serum.dat", header=T)
aserum
#
Check that the data are the same as given in the table in the exercise
#
Make sure that you understand how the data are coded.
#
Attach the data frame
attach(aserum)
# Plot serum response against time for the different persons (identified by colors) and treatments
# (identified by solid or dotted line) using the matplot command for multiple lines in a plot.
hours.mat<- matrix(c(1,2,3,6), nrow=4,ncol=10)
druga<-matrix(aserum$serum[drug==1], nrow=4)
drugb<-matrix(aserum$serum[drug==2], nrow=4)
serum.mat<-cbind(druga, drugb)
matplot(hours.mat, serum.mat, type="l", lty=c(1,1,1,1,1,2,2,2,2,2), col=c(1,2,3,4,5,1,2,3,4,5))
# Think about what the plot tells you!
# QUESTION b)
# This is a theoretical question (i.e. a discussion question)
# Here are three possible ways to approach the problem (cf.
# (i) Consider the model y_ijt = a_ij + b_ij * t + e_ijt, where i in 1:5 is the subject number,
# j in 1:2 is the drug number, and t in c(1,2,3,6) is time.
# Assume a_ij~N(alfa_j, sigma^2) and b_ij~N(beta_j, tau^2).
# Check if alfa_1=alfa_2 or ?beta_1=beta_2. (Most interesting for the betas.)
# (ii) Same as above, but with ante-dependance:
# y_ijt = a_ij + b_ij * t + c_ij * y_ij,t-1 + e_ijt
# Assumme again the the coefficients are drawn from the same distribution for each drug,
# and check if those distributions are equal.
# (iii) Three-way ANOVA, with drug, time and individuals as factors
# QUESTION c)
# Here we consider approach (i) discussed above.
# Give the commands:
druga.coef<-array(NA,c(2,5))
drugb.coef<-array(NA,c(2,5))
hours<-c(1,2,3,6)
for(i in 1:5)
{
? druga.coef[,i]<-lm(druga[,i] ~hours)$coef
?drugb.coef[,i]<-lm(drugb[,i] ~hours)$coef
}
druga.coef
drugb.coef
# Make sure you understand the commands and what is computed!
# Make t-tests based on the estimated slopes? for each individual:
t.test(druga.coef[2,], drugb.coef[2,], var.equal=T)
# What you may conclude from this hypothesis testing?
# QUESTION d)
# Here we define? z_it=y_i2t - y_i1t, and consider the model z_it = a + b * t + noise
# Nullhypothesis: a=0 and b=0? (Of most interest: b=0)
# Give the commands:
drug.diff<- druga-drugb
diff.coef<-array(NA,c(2,5))
for(i in 1:5) diff.coef[,i]<-lm(drug.diff[,i]~hours)$coef
t.test(diff.coef[2,])
# Make sure you understand the commands and what is computed!
# What you may conclude from this hypothesis testing?
# QUESTION e)
# We may do a three-way analysis of variance:
mod1<-lm(serum~factor(subject)+factor(time)+factor(drug))
anova(mod1,test="Chi")
# What can you conclude from this anova analysis?
# QUESTION f)
# Summarise your conclusions.
# Are the analyses consistent when it comes to the assessment of the treatment effect
# Are there effects of subjects and/or time?