


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



################One-Way MANOVA######## 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 : X[,1:3]=log(X[,1:3]) #authors suggest to transform the data ###use stacked observations (do not separate into different datasets) Y=as.matrix(X[,1:3]) ###No. of observations, No. of variables n=nrow(Y) p=ncol(Y) n1=table(X$Gender)[1] n2=table(X$Gender)[2] ####Design matrix#### ####Code variables in the FULL model: ####column1: all 1(for intercept term), ####column2: 1=female, - 1=male Z=matrix(0,ncol=2,nrow=n) Z[,1]=rep(1,n) Z[X$Gender=='female',2]=rep(1,n1) Z[X$Gender=='male',2]=rep(-1,n2) > Z [,1] [,2] [1,] 1 1 [2,] 1 1 [3,] 1 1 : [25,] 1 - 1 [26,] 1 - 1 [27,] 1 - 1 : #####Columns of Z in full model####### i_F=c(1,2) g=length(i_F) #####Columns of Z in reduced model####### i_R= g1=length(i_R)
################################
################################
###Regression Estimator---Full model####
##W=(Y-Z !ˆ^ )'(Y-Z !ˆ^ ) #- Error sum of squares and cross products matrix ##Yhat=Z !ˆ^ #- Predicted values ##E=Y-Yhat #- Residuals ###Full model Treatment sum of squares Bhat=solve(t(Z[,i_F]) %% Z[,i_F]) %% t(Z[,i_F]) %% Y ###Full model Error sum of squares and cross products matrix W=t(Y - Z[,i_F] %% Bhat) %% (Y - Z[,i_F] %% Bhat) ###Error covariance estimate S=W/(n-g) ########Reduced model estimate##### Bhat1=solve(t(Z[,i_R]) %%Z [,i_R]) %% t(Z[,i_R]) %% Y ###Reduced model error sum of products and cross products matrix G=t(Y - Z[,i_R] %% Bhat1) %% (Y - Z[,i_R] %% Bhat1) #####Hypothesis error sum of products B=G-W Lambda=det(W)/(det(B+W)) > Lambda [1] 0. ######### p=3,g=2, use Item 3 in Table 6.3; also, n= (1-Lambda)/Lambda*((n-p-1)/p) [1] 27. ####(27.11803 > F_{0.01, 3, 44}=4.26) qf(0.99,p,n-p-1)
################# Example 6.9 ################## XX=data.frame(x1=c(9,6,9,0,2,3,1,2), x2=c(3,2,7,4,0,8,9,7), factor=as.factor(c(1,1,1,2,2,3,3,3)) ) > XX x1 x2 factor 1 9 3 1 2 6 2 1 3 9 7 1 4 0 4 2 5 2 0 2 6 3 8 3 7 1 9 3 8 2 7 3 ###use stacked observations (do not separate into different datasets) YY=as.matrix(XX[,1:2]) ###No. of observations, No. of variables n=nrow(YY) p=ncol(YY) n1=table(XX$factor)[1] n2=table(XX$factor)[2] n3=table(XX$factor)[3]
#####Columns of Z in full model####### i_F=c(1,2,3) g=length(i_F) #####Columns of Z in reduced model####### i_R= g1=length(i_R) ###Full model Treatment sum of squares Bhat=solve(t(Z[,i_F]) %% Z[,i_F]) %% t(Z[,i_F]) %% YY ###Full model Error sum of squares and cross products matrix W=t(YY - Z[,i_F] %% Bhat) %% (YY - Z[,i_F] %% Bhat) ###Error covariance estimate S=W/(n-g) ########Reduced model estimate##### BhatR=solve(t(Z[,i_R]) %%Z [,i_R]) %% t(Z[,i_R]) %% YY ###Reduced model error sum of products and cross products matrix G=t(YY - Z[,i_R] %% BhatR) %% (YY - Z[,i_R] %% BhatR) #####Hypothesis error sum of products B=G-W Lambda=det(W)/(det(B+W)) > Lambda [1] 0.
(1-sqrt(Lambda))/sqrt(Lambda)((n-g-1)/(g-1)) [1] 8. ####(8.19886 > F_{0.01, 4, 8}=7.01) qf(0.99,2(g-1),2*(n-g-1))