Docsity

Project 2 for Multivariable Data Analysis | STA 135, Study Guides, Projects, Research of Descriptive statistics

Toggle sidebar
bg1
1
Sta 135 - Project 2
The project has two parts. The first part involves a multivariate multiple regression
analysis. The second part is related to principal component analysis.
Part I
A data set is provided in Problem 7.26 on measurements of pulp fibers and paper
produced from them. The multivariate response variables are y1breaking length,
y2elastic modulus, y3stress at failure, y4burst strength, and the predictors are
z1arithmetic fiber length, z2long fiber fraction, z3fine fiber fraction, z4zero span
tensile.
Optional. Exploratory analysis. We examine the scatterplot of the data to see the
associations between variables to get some insight what models might be appropriate
for these data. In addition, we compute correlation coefficients between all variables.
R=
y1y2y3y4z1z2z3z4
y11 0.91 0.98 0.99 0.65 0.74 −0.54 0.82
y20.91 1 0.94 0.87 0.54 0.61 −0.56 0.85
y30.98 0.94 1 0.97 0.68 0.76 −0.57 0.87
y40.99 0.87 0.97 1 0.71 0.8−0.56 0.81
z10.65 0.54 0.68 0.71 1 0.91 −0.73 0.78
z20.74 0.61 0.76 0.8 0.91 1 −0.71 0.79
z3−0.54 −0.56 −0.57 −0.56 −0.73 −0.71 1 −0.78
z40.82 0.85 0.87 0.81 0.78 0.79 −0.78 1
Figure 1 indicates that all response variables have strong linear correlation between
one another r > .87. All response variables have fairly high linear association with
all predictor variables, among which variable z4shows the strongest positive linear
association with all predictor variables. It appears that all four predictor variables
may help to explain the variability in response. However, all predictor variables have
strong linear correlation between one another as well. Thus multicollinearity could
be an issue in the further analysis of these data, which may lead to weak predictive
ability of the model (due to large variance of regression coefficients). In further
studies, we suggest to either deleting one of a pair of predictor variables that are
strongly correlated, or summarize the predictors by the principal components and
relate the response to these PCs.
1.1. We carry out a multivariate multiple regression analysis with y= (y1,y2, y3, y4)
as the response and z1, z2, z3, z4as the predictors. We following model is assummed:
Y=Zβ +,
where E((j))=0, Cov ((j), (k)) = σjkI, and have normal distribution. The dimen-
sions of matrices are: Yand are (62 ×4), Zis (62 ×5), βis (5 ×4).
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Project 2 for Multivariable Data Analysis | STA 135 and more Study Guides, Projects, Research Descriptive statistics in PDF only on Docsity!

Sta 135 - Project 2

The project has two parts. The first part involves a multivariate multiple regression

analysis. The second part is related to principal component analysis.

Part I

A data set is provided in Problem 7.26 on measurements of pulp fibers and paper

produced from them. The multivariate response variables are y 1 breaking length,

y 2 elastic modulus, y 3 stress at failure, y 4 burst strength, and the predictors are

z 1 arithmetic fiber length, z 2 long fiber fraction, z 3 fine fiber fraction, z 4 zero span

tensile.

Optional. Exploratory analysis. We examine the scatterplot of the data to see the

associations between variables to get some insight what models might be appropriate

for these data. In addition, we compute correlation coefficients between all variables.

R =

y 1 y 2 y 3 y 4 z 1 z 2 z 3 z 4

y 1 1 0. 91 0. 98 0. 99 0. 65 0. 74 − 0. 54 0. 82

y 2 0. 91 1 0. 94 0. 87 0. 54 0. 61 − 0. 56 0. 85

y 3 0. 98 0. 94 1 0. 97 0. 68 0. 76 − 0. 57 0. 87

y 4 0. 99 0. 87 0. 97 1 0. 71 0. 8 − 0. 56 0. 81

z 1 0. 65 0. 54 0. 68 0. 71 1 0. 91 − 0. 73 0. 78

z 2 0. 74 0. 61 0. 76 0. 8 0. 91 1 − 0. 71 0. 79

z 3 − 0. 54 − 0. 56 − 0. 57 − 0. 56 − 0. 73 − 0. 71 1 − 0. 78

z 4 0. 82 0. 85 0. 87 0. 81 0. 78 0. 79 − 0. 78 1

Figure 1 indicates that all response variables have strong linear correlation between

one another r > .87. All response variables have fairly high linear association with

all predictor variables, among which variable z 4 shows the strongest positive linear

association with all predictor variables. It appears that all four predictor variables

may help to explain the variability in response. However, all predictor variables have

strong linear correlation between one another as well. Thus multicollinearity could

be an issue in the further analysis of these data, which may lead to weak predictive

ability of the model (due to large variance of regression coefficients). In further

studies, we suggest to either deleting one of a pair of predictor variables that are

strongly correlated, or summarize the predictors by the principal components and

relate the response to these PCs.

1.1. We carry out a multivariate multiple regression analysis with y = (y 1 , y 2 , y 3 , y 4 )

as the response and z 1 , z 2 , z 3 , z 4 as the predictors. We following model is assummed:

Y = Zβ + ,

where E((j)) = 0, Cov((j), (k)) = σjkI, and  have normal distribution. The dimen-

sions of matrices are: Y and  are (62 × 4), Z is (62 × 5), β is (5 × 4).

y

6.0 7.0 8.0 -0.5 1.0 0 20 50 1.00 1.

16

20

24

y

y

3

5

7

-0.

1.0 y

z

-0.

0

20

50

z

z

0

40

80

16 20 24

3 5 7 -0.6 0.0 0.6 0 40 80

z

Figure 1: Pairwise Scatterplots of the data

Covˆ ( βˆ (2),^ βˆ (2)) =

Cov^ ˆ ( βˆ (2),^ βˆ (3)) =

Cov^ ˆ ( βˆ (2),^ βˆ (4)) =

Cov^ ˆ ( βˆ (3),^ βˆ (3)) =

Cov^ ˆ ( βˆ (3),^ βˆ (4)) =

Cov^ ˆ ( βˆ (4),^ βˆ (4)) =

1.2. We analyze the residuals of the fitted model and check for outliers. Figure 2

shows the results. The residual plots versus the fitted values appear to have points

randomly spread out above and below the reference line. The normal probability plots

(Q-Q plots) of residuals show majority of points lie along a straight line, except for the

tails in residuals 1, 3 and 4, which shows small evidence against normality assumption

of the error terms. Three outliers (observations with large standardized residuals) are

detected to be observations #51, #52, #56. Apart from outliers, residual plots look

good, and no serious violations of model assumptions are detected. (One should

consider to check the residual plot after removing the outliers to make sure these

observations do not significantly affect the model fit.)

Optional. We also examine the leverage points which may exert a strong pull on the

fit of the model. Leverage points are defined as hjj = diag(H) = diag(Z(Z

′ Z)

− 1 Z

′ ).

Observations with high leverage include #46, #57, #58, #60, #61. (Those are cho-

sen as twice larger than the average leverage). Found outliers are not leveraging

observations.

16 18 20 22 24 26

0

2

Residual vs Predicted values (for Response 1)

Yhat

Residulas

6.0 6.5 7.0 7.5 8.0 8.

-0.

Residual vs Predicted values (for Response 2)

Yhat

Residulas

3 4 5 6 7 8

-2.

-1.

Residual vs Predicted values (for Response 3)

Yhat

Residulas

-0.5 0.0 0.5 1.0 1.5 2.

-1.

-0.

Residual vs Predicted values (for Response 4)

Yhat

Residulas

-2 -1 0 1 2

0

2

Residual Q-Q plot (for Response 1)

Theoretical Quantiles

Sample Quantiles^ Obs. Obs. Obs.

-2 -1 0 1 2

-0.

Residual Q-Q plot (for Response 2)

Theoretical Quantiles

Sample Quantiles^ Obs. Obs. Obs.

-2 -1 0 1 2

-2.

-1.

Residual Q-Q plot (for Response 3)

Theoretical Quantiles

Sample Quantiles^ Obs. Obs. Obs.

-2 -1 0 1 2

-1.

-0.

Residual Q-Q plot (for Response 4)

Theoretical Quantiles

Sample Quantiles^ Obs. Obs. Obs.

Figure 2: Residual plots

1.3. We construct a 95% prediction ellipsoid for a future response, Y 0 , that corre-

sponds to z 0 = (1, z 01 , z 02 , z 03 , z 04 ) = (1, 0. 1540 , 49. 9146 , 36. 5140 , 1 .0912), where z 0 j

is the 80th percentile of the observed zj ’s, 1 ≤ j ≤ 4.

The 95% prediction ellipse for future response is determined by all Y 01 ,... , Y 04 such

that:

Y 01 − 24. 8

Y 02 − 7. 94

Y 03 − 7. 25

Y 04 − 1. 74

Y 01 − 24. 8

Y 02 − 7. 94

Y 03 − 7. 25

Y 04 − 1. 74

≤ (1 + z

′ 0 (Z

′ Z)

− 1 z 0 )

F (.95; 4, 54) = 11. 7936

We also construct 95% simultaneous prediction intervals for the components of Y 0

with the same z 0.

Y 01 : [19. 6531 , 29 .9419]

Y 02 : [6. 7620 , 9 .1199]

Y 03 : [5. 0316 , 9 .4767]

Y 04 : [0. 5450 , 2 .9408]

The 95% prediction ellipse is defined in a 4-dimensional space, thus it is impossible

to visualize directly. One could consider to visualize a projection of this ellipse to a

two-dimensional space, however one must remember that a lot information could be

lost due to such projection. The result is shown in Figure 3 together with the 95%

Association between all pairs of response variables has been slightly reduced after

emilinating the effects of the four predictor variables on each pair of responses. It

appears that the predictors have little effect on the association between response

variables. The response variables are highly linearly accossiated even after removing

the effects of predictors.

1.5. We apply the AIC to perform a variable selection among the four predictors,

z 1 , z 2 , z 3 , z 4.

Model for response j = 1, 2 , 3 , 4 AIC

Yj = β 0 j + j − 516. 6164

Yj = β 0 j + β 1 j z 1 + j − 574. 6033

Yj = β 0 j + β 1 j z 2 + j − 605. 7228

Yj = β 0 j + β 1 j z 3 + j − 539. 5914

Yj = β 0 j + β 1 j z 4 + j − 602. 7387

Yj = β 0 j + β 1 j z 1 + β 2 j z 2 + j − 598. 3503

Yj = β 0 j + β 1 j z 1 + β 2 j z 3 + j − 579. 0143

Yj = β 0 j + β 1 j z 1 + β 2 j z 4 + j − 643. 3927

Yj = β 0 j + β 1 j z 2 + β 2 j z 3 + j − 611. 5410

Yj = β 0 j + β 1 j z 2 + β 2 j z 4 + j − 661. 2189

Yj = β 0 j + β 1 j z 3 + β 2 j z 4 + j − 610. 1177

Yj = β 0 j + β 1 j z 1 + β 2 j z 2 + β 3 j z 3 + j − 606. 4455

Yj = β 0 j + β 1 j z 1 + β 2 j z 2 + β 3 j z 4 + j − 665. 3869

Yj = β 0 j + β 1 j z 1 + β 2 j z 3 + β 3 j z 4 + j − 648. 0075

Yj = β 0 j + β 1 j z 2 + β 2 j z 3 + β 3 j z 4 + j − 669. 5558

Yj = β 0 j + β 1 j z 1 + β 2 j z 2 + β 3 j z 3 + β 4 j z 4 + j − 672. 1140

The best model that is chosen by the AIC is the full model with 4 predictor variables

Yj = β 0 j + β 1 j z 1 + β 2 j z 2 + β 3 j z 3 + β 4 j z 4 + j , fitted in part 1.1.

Part II

A second data set is provided in Problem 8.28 represents survey data that were

collected as part of a study to assess options for enhansing food security through

the sustainable use of natural resources in the Sikasso region of Mali (West Afrika).

A total of n = 76 farmers were surveyed and observations on the nine variables: x 1

total number of individuals in household, x 2 distance in kilometers to nearest passable

road, x 3 hectares of cotton, x 4 hectares of maize, x 5 hectares of sorghum, x 6 hectares

of millet, all planted in year 2000, x 7 total number of bullocks or draft anumals, x 8

tocal cattle, x 9 total goats. We perform a principal component analysis (PCA) to

find relationships between variables in farm data.

2.1. The PCA is based on the sample correlation matrix, R.

R =

Its eigenvalues and eigenvectors are:

λ 1 = 4.1443, λ 2 = 1.2364, λ 3 = 1.0581, λ 4 = 0.9205,

λ 5 = 0.6058, λ 6 = 0.5044, λ 7 = 0.2720, λ 8 = 0.1470, λ 9 = 0.1114,

Variable ˆe 1 ˆe 2 eˆ 3 eˆ 4 ˆe 5 ˆe 6 eˆ 7 ˆe 8 ˆe 9

Family 0. 444 0. 1 0. 002 − 0. 123 0. 089 0. 127 0. 579 − 0. 454 0. 461

DistRd − 0. 033 0. 072 0. 831 0. 502 0. 194 0. 051 0. 045 − 0. 082 − 0. 041

Cotton 0. 411 0. 342 0. 068 0. 03 − 0. 1 0. 216 − 0. 509 0. 372 0. 504

Maise 0. 337 0. 554 − 0. 17 0. 164 0. 134 − 0. 053 0. 352 0. 36 − 0. 499

Sorg 0. 311 − 0. 452 0. 069 − 0. 229 0. 361 0. 632 − 0. 055 0. 139 − 0. 3

Millet 0. 269 − 0. 043 0. 385 − 0. 606 0. 182 − 0. 594 − 0. 089 0. 097 − 0. 077

Bull 0. 44 0. 029 − 0. 122 0. 197 − 0. 129 − 0. 11 − 0. 458 − 0. 621 − 0. 357

Cattle 0. 247 − 0. 458 − 0. 278 0. 486 0. 392 − 0. 407 0. 012 0. 215 0. 225

Goats 0. 309 − 0. 379 0. 173 0. 1 − 0. 77 − 0. 043 0. 242 0. 242 − 0. 095

The cumulative percentages of total variability of 9 eigenvectors are: 46.05%, 59.79%,

71 .54%, 81.77%, 88.50%, 94.11%, 97.13%, 98.76%, 100.00%.

The top PCs that summarize 80% - 90% of the total variability are the first 5 eigen

vectors.

The total variability is

λj = p = 9.

We also use a scree plot(optional) to help select the number of principal components.

Figure 4 shows that the choice of 5 PCs is reasonable.

Using standardized variables zj =

xj −¯xj √ sjj , the first 5 principal components are ˆyj^ = ˆej^ z:

yˆ 1 = 0. 444 z 1 − 0. 033 z 2 + 0. 411 z 3 + 0. 337 z 4 + 0. 311 z 5 + 0. 269 z 6 + 0. 440 z 7 + 0. 247 z 8 + 0. 309 z 9

y ˆ 2 = 0. 100 z 1 + 0. 072 z 2 + 0. 342 z 3 + 0. 554 z 4 − 0. 452 z 5 − 0. 043 z 6 + 0. 029 z 7 − 0. 458 z 8 − 0. 379 z 9

y ˆ 3 = 0. 002 z 1 + 0. 831 z 2 + 0. 068 z 3 − 0. 170 z 4 + 0. 069 z 5 + 0. 385 z 6 − 0. 122 z 7 − 0. 278 z 8 + 0. 173 z 9

y ˆ 4 = − 0. 123 z 1 + 0. 502 z 2 + 0. 030 z 3 + 0. 164 z 4 − 0. 229 z 5 − 0. 606 z 6 + 0. 197 z 7 + 0. 486 z 8 + 0. 100 z 9

y ˆ 5 = 0. 089 z 1 + 0. 194 z 2 − 0. 100 z 3 + 0. 134 z 4 + 0. 361 z 5 + 0. 182 z 6 − 0. 129 z 7 + 0. 392 z 8 − 0. 770 z 9

millet and sorghum. This component might be interpreted as a contrast between farm

utility and grain.

The fifth component appears to contrast sorghum and cattle versus goats. This com-

ponent appears to be a contrast within syrup, milk and meat production, however

the direct relationships are not clear.

2.3. The reason of using R, instead of S, the sample covariance matrix, for PCA is

that the use of R provides for fair variable comparisons due to standardization each

variable. When the original variables are not of the same scale, the large variance

of large scale variables will dominate the proportion of total variability, which leads

the first few principal components to select these large scale variables, instead of the

relationships among other variables.

As a comparison, we repeat part 2.1 with R replaced by S. The PCA is based on the

sample correlation matrix, S.

S =

Its eigenvalues and eigenvectors are:

λ 1 = 6538.8594, λ 2 = 590.1075, λ 3 = 147.5506, λ 4 = 12.7110,

λ 5 = 5.8905, λ 6 = 3.9910, λ 7 = 2.9594, λ 8 = 1.1372, λ 9 = 0.6913,

Variable ˆe 1 ˆe 2 ˆe 3 ˆe 4 eˆ 5 ˆe 6 ˆe 7 eˆ 8 eˆ 9

Family 0. 027 0. 954 − 0. 252 − 0. 091 − 0. 09 0. 091 − 0. 011 0. 011 0. 039

DistRd − 1. 000 0. 028 0. 004 − 0. 004 − 0. 002 0. 001 0. 001 0. 002 − 0. 001

Cotton − 0. 001 0. 084 − 0. 039 0. 074 0. 654 − 0. 366 0. 199 − 0. 468 0. 407

Maize 0. 001 0. 051 − 0. 019 − 0. 129 0. 410 0. 080 − 0. 085 − 0. 274 − 0. 851

Sorg 0. 002 0. 058 0. 031 0. 114 − 0. 350 − 0. 543 0. 689 − 0. 006 − 0. 301

Millet − 0. 000 0. 046 − 0. 029 0. 086 − 0. 174 − 0. 693 − 0. 686 − 0. 038 − 0. 078

Bull 0. 003 0. 084 0. 043 0. 146 0. 493 − 0. 186 0. 047 0. 828 − 0. 069

Cattle 0. 011 0. 244 0. 963 − 0. 085 − 0. 004 0. 004 − 0. 038 − 0. 053 0. 025

Goats − 0. 001 0. 089 0. 054 0. 959 − 0. 022 0. 203 − 0. 059 − 0. 127 − 0. 087

The cumulative percentages of total variability of 9 eigenvectors are: 89.53%, 97.60%,

99 .63%, 99.80%, 99.88%, 99.93%, 99.97%, 99.99%, 100.00%.

The top PCs that summarize 80% - 90% of the total variability is the first eigen vector.

The total variability is

λi = 7303.898.

We also use a scree plot(optional) to help select the number of principal components.

Figure 5 shows that the choice of the first PC is reasonable.

1 2 3 4 5 6 7 8 9

0

2000

4000

6000

Scree plot (based on S)

i

Lambda_i

Figure 5: Scree plot

The first principal component is ˆy 1 = ˆe 1 x:

yˆ 1 = 0. 027 x 1 − 1. 000 x 2 − 0. 001 x 3 + 0. 001 x 4 + 0. 002 x 5 − 0. 000 x 6 + 0. 003 x 7 + 0. 011 x 8 − 0. 001 x 9

Optional. The correlation coefficients corresponding to the first PC are:

Variable ryˆ 1 ,x k Family 0. 09

DistRd − 1. 00

Cotton − 0. 02

Maize 0. 06

Sorg 0. 08

Millet − 0. 02

Bull 0. 09

Cattle 0. 07

Goats − 0. 03

Conclusion: The result is different from PCA applies to R. Since the scale of the

variable distance to road is much larger than the scale of other variables, its variance

dominates to total variability, thus the first principal component loads the distance

to road variable unanimously and explains 89.53% of the total variability.

main=c("Residual vs Predicted values",

paste(c("(for Response ",j,")"),collapse=""))) abline(h= 0 )

points(Yhat[out,j],Res[out,j],

col=c( 2 , 3 , 4 ),pch=c( 16 , 17 , 15 ),cex=1.3)

}

for(j in 1 : 4 ){

qqnorm(Res[,j], main=c("Residual Q-Q plot",

paste(c("(for Response ",j,")"),collapse=""))) qqline(Res[,j])

QQ=qqnorm(Res[,j],plot.it=FALSE) points(QQ$x[out],QQ$y[out],

col=c( 2 , 3 , 4 ),pch=c( 16 , 17 , 15 ),cex=1.3)

legend("bottomright",legend=c("Obs.52","Obs.56","Obs.51"),

col=c( 2 , 3 , 4 ),pch=c( 16 , 17 , 15 ))

}

par(mfrow=c( 1 , 1 ))

Optional part: Leverages

h=diag( Z%%solve(t(Z)%%Z)%*%t(Z) )

sort(h)

order(h)

which( sum(h)/n* 2 < h)

#Choose High Leverage Obs: 57 58 46 61 60,

#Choose Large Leverage Obs: 48 59 57 58 46 61 60

1.

0.8*n #equal 49.6th sorted value of z_i

#method 1: Choose the 50th Observation

z0=(apply(Z[,- 1 ], 2 ,sort))[ 50 ,]

#method 2: Choose the 49.6th Observation by linear interpolation

z0=(apply(Z[,- 1 ], 2 ,sort))[ 49 ,]+

0.6*((apply(Z[,- 1 ], 2 ,sort))[ 50 ,]-(apply(Z[,- 1 ], 2 ,sort))[ 49 ,] )

z0=c( 1 ,z0)

z

predMean=t(z0)%*%B

round(predMean, 2 )

round(solve(S), 2 )

c2=( 1 +t(z0)%%solve(t(Z)%%Z)%%z0)m(n-r- 1 )/(n-r-m)qf(0.95,m,n-r-m)

c

#Prediction Interval

f95=qf(0.95,m,n-r-m)

TT = matrix( 0 ,ncol= 2 ,nrow=m)

for(j in 1 :m){

u = predMean[j] se = sqrt( S[j,j] * ( 1 +t(z0)%%solve(t(Z)%%Z)%*%z0) )

ds = sqrt( f95 * m(n-r- 1 )/(n-r-m) )se

TT[j,] = c(u-ds,u+ds)

}

TT

#plot 2 - D view of prediction ellipse, plut the Prediction Intervals

which=matrix(c( 1 , 2 , 1 , 3 , 1 , 4 , 2 , 3 , 2 , 4 , 3 , 4 ),

ncol= 2 ,byrow=TRUE)

munames=c('Yo1','Yo2','Yo3','Yo4')

par(mfrow=c( 2 , 3 ))

c2=( 1 +t(z0)%%solve(t(Z)%%Z)%%z0) * m(n-r- 1 )/(n-r-m) * qf(.95,m,n-r-m)

for(j in 1 : 6 ){

k=which[j,] #which two variables to use

eig=eigen(solve(S[k,k]))

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 )* 2 *pi/ 100

mu1=e1V1[ 1 ]cos(angle) + e2V2[ 1 ]sin(angle) + predMean[k[ 1 ]] mu2=e1V1[ 2 ]cos(angle) + e2V2[ 2 ]sin(angle) + predMean[k[ 2 ]]

plot(mu1,mu2,type="l", xlab=munames[k[ 1 ]],ylab=munames[k[ 2 ]],lwd= 2 )

abline(v=predMean[k[ 1 ]],lty='twodash',col='green') abline(h=predMean[k[ 2 ]],lty='twodash',col='green')

points(predMean[k[ 1 ]],predMean[k[ 2 ]],col='green',pch= 19 )

lines(ellipse(S[k,k]/n,centre=predMean[k],level=0.95),type='l', lty='dotted', col='red')

lines(TT[k[ 1 ],],rep(TT[k[ 2 ], 1 ], 2 ),lty='longdash',col='blue',lwd= 2 ) lines(TT[k[ 1 ],],rep(TT[k[ 2 ], 2 ], 2 ),lty='longdash',col='blue',lwd= 2 )

lines(rep(TT[k[ 1 ], 1 ], 2 ),TT[k[ 2 ],],lty='longdash',col='blue',lwd= 2 ) lines(rep(TT[k[ 1 ], 2 ], 2 ),TT[k[ 2 ],],lty='longdash',col='blue',lwd= 2 )

}

par(mfrow=c( 1 , 1 ))

title(main="2-D view 95% prediction ellipse and prediction intervals for the response")

1.

#Check the variance estimate (divide by n? or n-1?)

round( cor(Y), 4 )

round( var(data), 4 )

Syy=var(data)[ 1 : 4 , 1 : 4 ] #also equal to: var(Y)

Syy=var(Y)

Syz=var(data)[ 1 : 4 , 5 : 8 ]

Szz=var(data)[ 5 : 8 , 5 : 8 ] #also equal to: var(Z[,-1])

Szz=var(Z[,- 1 ])

Syy.z = Syy - Syz%%solve(Szz)%%t(Syz)

round( Syy.z, 4 )

#for (j,k) partial correlation:

#Syy.z[j,k]/sqrt(Syy.z[j,j]*Syy.z[k,k])

for(j in 1 : 3 ){

for(k in (j+ 1 ): 4 ){

print(c(j,k)) print(round( Syy.z[j,k]/sqrt(Syy.z[j,j]*Syy.z[k,k]), 4 ))

##compare cor(Y) and cor(Y | Z) side by side

#print(round(c(cor(Y)[j,k], Syy.z[j,k]/sqrt(Syy.z[j,j]*Syy.z[k,k])), 4)) }

}

1.

#Store all AIC results into a table

result=data.frame(z=t(rep( 0 , 4 )), param= 0 , AIC= 0 )

#get AIC of all models

idx=matrix(c( 0 , 0 , 0 , 0 , #index of which predictors to use

1 , 0 , 0 , 0 , 0 , 2 , 0 , 0 , 0 , 0 , 3 , 0 , 0 , 0 , 0 , 4 , 1 , 2 , 0 , 0 , 1 , 0 , 3 , 0 , 1 , 0 , 0 , 4 , 0 , 2 , 3 , 0 , 0 , 2 , 0 , 4 , 0 , 0 , 3 , 4 ,

1 , 2 , 3 , 0 , 1 , 2 , 0 , 4 , 1 , 0 , 3 , 4 , 0 , 2 , 3 , 4 , 1 , 2 , 3 , 4 ), byrow=TRUE, ncol= 4 )

for(j in 1 : 16 ){

Z=cbind( 1 ,as.matrix(data[, 5 : 8 ])[,idx[j,]])

B=solve(t(Z)%%Z)%%t(Z)%%Y Yhat=Z%%B

d=ncol(Z)

AIC=AIC = nlog(det( t(Y-Yhat)%%(Y-Yhat)/n )) + 2 m(d) result[j,]=c(idx[j,], d, AIC)

}

result

Part 2

data=read.table("Sta135jiming/Wichern_data/T8-7.dat")

names(data)=c("Family","DistRd","Cotton","Maize","Sorg","Millet","Bull","Cattle","Goats")

2.

R=cor(data)

round(R, 4 )