R partial regression plots

From InterSciWiki
Jump to: navigation, search

partial.regression.plot <- function (y, x, n, ...) {

  m <- as.matrix(x[,-n])
  y1 <- lm(y ~ m)$res
  x1 <- lm(x[,n] ~ m)$res
  plot( y1 ~ x1, ... )
  abline(lm(y1~x1), col='red')
}

n <- 100
x1 <- rnorm(n)
x2 <- rnorm(n)
x3 <- x1+x2+rnorm(n)
x <- cbind(x1,x2,x3)
y <- x1+x2+x3+rnorm(n)
op <- par(mfrow=c(2,2))
partial.regression.plot(y, x, 1)
partial.regression.plot(y, x, 2)
partial.regression.plot(y, x, 3)
par(op)

Evileye=sccs$v1188, Moralgods=sccs$v238, Milking=sccs$v245

partial.regression.plot <- function (y, x, n, ...) {
  m <- as.matrix(x[,-n])
  y1 <- lm(y ~ m)$res
  x1 <- lm(x[,n] ~ m)$res
  plot( y1 ~ x1, ... )
  abline(lm(y1~x1), col='red')
}

Milking=sccs$v245
Moralgods=sccs$v238
Evileye=sccs$v1188
n <- 186
rnorm=rnorm(n) 
x1 <- Milking
x2 <- Moralgods
x3 <- Evileye #x1+x2+
x <- cbind(x1,x2,x3)#,rnorm)
y <- x1+x2+x3  #+rnorm(n)
op <- par(mfrow=c(2,2))
partial.regression.plot(y, x, 1) #x1
partial.regression.plot(y, x, 2) #x2
partial.regression.plot(y, x, 3) #Evileye #+x1+x2
par(op)
xx1=x[,1]
xx2=x[,2]
xx3=x[,3]
partial.regression.plot(xx1, x, 2) #x1
partial.regression.plot(xx1, x, 3) #x2
partial.regression.plot(xx2, x, 3) #Evileye #+x1+x2
par(op)


x1 <- Milking
x2 <- Moralgods
x3 <- x1+x2+Evileye
x <- cbind(x1,x2,x3)#,rnorm)
y <- x1+x2+x3  #+rnorm(n)
op <- par(mfrow=c(2,2))
partial.regression.plot(y, x, 1)
partial.regression.plot(y, x, 2)
partial.regression.plot(y, x, 3)
par(op)
Personal tools