Docsity

Statistical Analysis of Equal Treatments: Repeated Measures Design - Example 6.2 - Prof. J, Exams of Descriptive statistics

Toggle sidebar
bg1
### Testing fo r equal treatm ents (repeated measures des ign) Example 6 .2
###load data## ##
X=read.table(" Sta135jiming/W ichern_data/T6-2. dat")
names(X)=c("Tr 1","Tr2","Tr3" ,"Tr4")
X=as.matrix(X) #if you want to change from data.frame o bject to matri x object
> X
Tr1 Tr2 Tr3 Tr4
1 426 609 556 600
2 253 236 392 395
3 359 433 349 357
:
:
### No. of obs ervations, No. of variables
n=nrow(X)
p=ncol(X)
> n
[1] 19
> p
[1] 4
### Matrix of multiple compa rsions ((q-1) row s) ###
C=matrix( c(-1,-1,1, 1,1,-1,1,-1,1,-1,-1,1) , ncol=4, byrow=TRUE)
> C
[,1] [,2] [,3] [,4]
[1,] -1 -1 1 1
[2,] 1 -1 1 -1
[3,] 1 -1 -1 1
####Mean vecto rs############
xb=colMeans(X)
> xb
[1] 368.2105 4 04.6316 479.2632 502.8947
#####covarianc e matrix###### ###
S=cov(X)
> S
[,1] [,2] [ ,3] [,4]
[1,] 2819.287 3568.415 2943. 497 2295.357
[2,] 3568.415 7963.135 5303. 991 4065.459
[3,] 2943.497 5303.991 6851. 316 4499.640
[4,] 2295.357 4065.459 4499.64 0 4878.988
####### Test t ransformed mea ns against C*m u####
m0=c(0,0,0)
#### Hotelling 's T-square:Eq(6-16) ######
T2=n*t(C%*%xb-m0)%*%solve(C%*%S%*%t(C))%*%(C%*%xb-m0)
> T2
[,1]
[1,] 116.0163
############## Check if C mat rix is a contrast , then update p to q-1######## ##
m=nrow(C)
> m
[1] 3
if( sum(C%*%re p(1,p) == rep( 0,m)) == m ) p=p-1 ##used as q-1 in F formul as
> p
[1] 3
#####scale to F distribution ######
F=(n-p)*T2/(p* (n-1))
> F
[,1]
[1,] 34.37521
######P-value##########
pf(F,p,n-p,lower.tail=FALSE) #remember, p here = q-1, sin ce C is a cont rast
pf3

Partial preview of the text

Download Statistical Analysis of Equal Treatments: Repeated Measures Design - Example 6.2 - Prof. J and more Exams Descriptive statistics in PDF only on Docsity!

Testing for equal treatments (repeated measures design) Example 6.

###load data#### X=read.table("Sta135jiming/Wichern_data/T6-2.dat") names(X)=c("Tr1","Tr2","Tr3","Tr4") X=as.matrix(X) #if you want to change from data.frame object to matrix object

X Tr1 Tr2 Tr3 Tr 1 426 609 556 600 2 253 236 392 395 3 359 433 349 357 : :

No. of observations, No. of variables

n=nrow(X) p=ncol(X)

n [1] 19 p [1] 4

Matrix of multiple comparsions ((q-1) rows)

C=matrix( c(-1,-1,1,1,1,-1,1,-1,1,-1,-1,1), ncol=4, byrow=TRUE)

C [,1] [,2] [,3] [,4] [1,] - 1 - 1 1 1 [2,] 1 - 1 1 - 1 [3,] 1 - 1 - 1 1 ####Mean vectors############ xb=colMeans(X) xb [1] 368.2105 404.6316 479. 2632 502. #####covariance matrix######### S=cov(X) S [,1] [,2] [,3] [,4] [1,] 2819.287 3568.415 2943.497 2295. [2,] 3568.415 7963.135 5303.991 4065. [3,] 2943.497 5303.991 6851.316 4499. [4,] 2295.357 4065.459 44 99.640 4878. ####### Test transformed means against C*mu#### m0=c(0,0,0)

Hotelling's T-square:Eq(6-16)

T2=nt(C%%xb-m0)%%solve(C%%S%%t(C))%%(C%*%xb-m0)

T [,1] [1,] 116. ##############Check if C matrix is a contrast, then update p to q-1########## m=nrow(C) m [1] 3 if( sum(C%%rep(1,p) == rep(0,m)) == m ) p=p- 1 ##used as q- 1 in F formulas p [1] 3 #####scale to F distribution###### F=(n-p)T2/(p*(n-1)) F [,1] [1,] 34. ######P-value########## pf(F,p,n-p,lower.tail=FALSE) #remember, p here = q-1, since C is a contrast

[,1]

[1,] 3.317767e- 07 #########Bonferroni t_{n-1}(alpha/2m)######## t95=qt(0.05/(2*m),n-1,lower.tail=FALSE)

t [1] 2. ########T-square F_{alpha,p,n-p}############ f95=qf(0.05,p,n-p,lower.tail=FALSE) f [1] 3. #############Confidence intervals, using (6-18) and Bonferroni####

TT=matrix(0,ncol=2,nrow=3) Bon=matrix(0,ncol=2,nrow=3) for(j in 1:m){ u = C[j,]%%xb se = sqrt( C[j,]%% S %%(C[j,])/n ) ##sqrt variance of comparison means## ds = sqrt(p(n-1)/(n-p)f95)se TT[j,] = c(u-ds,u+ds) dt = t95*se Bon[j,] = c(u-dt,u+dt) }

TT

[,1] [,2] [1,] 135.65030 282. [2,] - 114.72708 - 5.37 818 [3,] - 78.72858 53.

Bon [,1] [,2] [1,] 150.51359 268. [2,] - 103.69556 - 16. [3,] - 65.42422 39. ###########Confidence ellipse of the first 2 variables######## library(ellipse) plot(ellipse((C%%S%%t(C))/n,centre=C%%xb,level=0.95,which=c(1,2), type='l') ####alternative (more exact)#### c2=p(n-1)qf(.95,p,n-p)/(n-p) Sd=(C%%S%%t(C))[1:2,1:2] #select variance of first 2 contrast variables Mean=(C%%xb)[1:2,] #select mean of first 2 contrast variables eig=eigen(solve(Sd/n)) V1=eig$vec[,1] V2=eig$vec[,2] e1=sqrt(1/eig$val[1]c2) e2=sqrt(1/eig$val[2]c2) angle=(0:100)2pi/ Cmu1=e1V1[1]cos(angle) + e2V2[1]sin(angle) + Mean[1] Cmu2=e1V1[2]cos(angle) + e2V2[2]sin(angle) + Mean[2] lines(Cmu1,Cmu2,type='l',col='blue') ###########Compare two population means, assume Sigma1 = Sigma2###### X=read.table("Sta135jiming/Wichern_data/T6-9.dat") names(X)=c("L","W","H","Gender") X L W H Gender 1 98 81 38 female 2 103 84 38 female 3 103 86 42 female : 25 93 74 37 male 26 94 78 35 male :