Tosh
Tosh

Reputation: 135

Obtaining scatter plot matrix of residuals

I wish to draw a scatter plot matrix of residuals in R for a longitudinal data analysis.

I wrote the given codes:

head(LungCapData2)

The data reads as follows:

     Age LungCap Height Gender Smoke
 1   9   3.124   57.0   female no
 2   8   3.172   67.5 female   no 
 3   7   3.160   54.5 female   no 
 4   9   2.674   53.0   male   no 
 5   9   3.685   57.0   male   no 
 6   8   5.008   61.0 female   no

I start to obtain the residuals using OLS:

lm1 <- lm(LungCap~Height, data=LungCapData)
lm1res<- resid(lm1)
roundht <- round(LungCapData2$Height)
Lung<-reshape(LungCapData2[c("Age","lm1res","roundht")],
          direction="wide",v.names="lm1res", timevar="roundht",idvar="Age")

The problem is with last piece of code for reshaping it into the matrix. I wish to get a scatter plot by

pairs(Lung)

But my Lung data is as follows:

head(Lung)
  Age
1   9
2   8
3   7
4   6
5   5
6   4
  lm1res.c(57, 68, 54, 53, 61, 58, 56, 60, 50, 59, 62, 49, 52, 48, 65, 66, 55, 51, 69, 63, 64, 47, 46, 70, 72, 71, 67, 74, 73)
1

Cant figure out where I went wrong.

Upvotes: 0

Views: 335

Answers (1)

Nar
Nar

Reputation: 658

Need to assign lmres and roundht to dataframe columns as you then refer to dataframe in reshape :

    LungCapData$lm1res<- resid(lm1)
    LungCapData$roundht <- round(LungCapData$Height)
    Lung<-reshape(LungCapData[c("Age","lm1res","roundht")],
                  direction="wide", v.names="lm1res",timevar="roundht",idvar="Age")
    pairs(Lung)

Upvotes: 1

Related Questions