mcGraph function

Illustrate multicollinearity in regression, part 1.

Illustrate multicollinearity in regression, part 1.

This is a set of functions that faciliates the examination of multicollinearity. Suppose the "true" relationship is y[i] = 0.2 * x1[i] + 0.2 * x2[i] + e where e is Normal(0, stde^2).

mcGraph1 draws the 3D regression space, but all of the points are illustrated "in" the flat plane of x1-x2 variables.

mcGraph1(x1, x2, y, x1lab, x2lab, ylab, ...) mcGraph2(x1, x2, y, rescaley = 1, drawArrows = TRUE, x1lab, x2lab, ylab, ...) mcGraph3( x1, x2, y, interaction = FALSE, drawArrows = TRUE, x1lab, x2lab, ylab, ... )

Arguments

  • x1: a predictor vector
  • x2: a predictor vector
  • y: the dependent variable
  • x1lab: label for the x1 axis, (the one called "xlab" inside persp)
  • x2lab: label for the x2 axis, (the one called "ylab" inside persp)
  • ylab: label for the y (vertical) axis (the one called "zlab" inside persp)
  • ...: additional parameters passed to persp
  • rescaley: a single scalar value or a vector of the same length as y.
  • drawArrows: TRUE or FALSE, do you want arrows from the plane to observed y?
  • interaction: a TRUE or FALSE request for inclusion of the x1-x2 interaction in the regression calculation

Returns

mcGraph1 and mcGraph2 return only the perspective matrix from persp. It is returned so that users can add additional embellishments on the 3D plot (can be used with trans3d)

mcGraph3 returns a list of 2 objects. 1) the fitted regression model2) the perspective matrix used with persp to draw the image.

Details

These functions are specialized to a particular purpose. If you just want to draw 3D regressions, look at plotPlane.

Examples

set.seed(12345) ## Create data with x1 and x2 correlated at 0.10 dat <- genCorrelatedData(rho=.1, stde=7) mcGraph1(dat$x1, dat$x2, dat$y, theta=20, phi=8, ticktype="detailed", nticks=10) set.seed(12345) ## Create data with x1 and x2 correlated at 0.10 dat <- genCorrelatedData(rho=.1, stde=7) ## This will "grow" the "cloud" of points up from the ## x1-x2 axis mcGraph2(dat$x1, dat$x2, dat$y, rescaley = 0.0, theta = 0) mcGraph2(dat$x1, dat$x2, dat$y, rescaley = 0.1, theta = 0) mcGraph2(dat$x1, dat$x2, dat$y, rescaley = 0.2, theta = 0) mcGraph2(dat$x1, dat$x2, dat$y, rescaley = 0.3, theta = 0) mcGraph2(dat$x1, dat$x2, dat$y, rescaley = 0.4, theta = 0) mcGraph2(dat$x1, dat$x2, dat$y, rescaley = 0.5, theta = 0) mcGraph2(dat$x1, dat$x2, dat$y, rescaley = 0.6, theta = 0) mcGraph2(dat$x1, dat$x2, dat$y, rescaley = 0.7, theta = 0) mcGraph2(dat$x1, dat$x2, dat$y, rescaley = 0.8, theta = 0) mcGraph2(dat$x1, dat$x2, dat$y, rescaley = 0.9, theta = 0) mcGraph2(dat$x1, dat$x2, dat$y, rescaley = 1, theta = 0) ##rotate this mcGraph2(dat$x1, dat$x2, dat$y, rescaley = 1, theta = 20) mcGraph2(dat$x1, dat$x2, dat$y, rescaley = 1, theta = 40) mcGraph2(dat$x1, dat$x2, dat$y, rescaley = 1, theta = 60) mcGraph2(dat$x1, dat$x2, dat$y, rescaley = 1, theta = 80) ## once they reach the top, make them glitter a while for(i in 1:20){ mcGraph2(dat$x1, dat$x2, dat$y, rescaley = runif(length(dat$x1), .9,1.1), theta = 0) } set.seed(12345) ## Create data with x1 and x2 correlated at 0.10 dat <- genCorrelatedData(rho=.1, stde=7) mcGraph3(dat$x1, dat$x2, dat$y, theta = 0) dat2 <- genCorrelatedData(rho = 0, stde = 7) mcGraph3(dat2$x1, dat2$x2, dat2$y, theta = 0, phi = 10) mcGraph3(dat2$x1, dat2$x2, dat2$y, theta = 30, phi = 10) mcGraph3(dat2$x1, dat2$x2, dat2$y, theta = -30, phi = 10) mcGraph3(dat2$x1, dat2$x2, dat2$y, theta = -30, phi = -10) mcGraph3(dat2$x1, dat2$x2, dat2$y, theta = -30, phi = -15) ## Run regressions with not-strongly correlated data modset1 <- list() for(i in 1:20){ dat2 <- genCorrelatedData(rho = .1, stde = 7) summary(lm( y ~ x1 + x2 , data = dat2)) modset1[[i]] <- mcGraph3(dat2$x1, dat2$x2, dat2$y, theta = -30) } ## Run regressions with strongly correlated data modset2 <- list() for(i in 1:20){ dat2 <- genCorrelatedData(rho = .981, stde = 7) summary(lm( y ~ x1 + x2 , data = dat2)) modset2[[i]] <- mcGraph3(dat2$x1, dat2$x2, dat2$y, theta = -30) } dat3 <- genCorrelatedData(rho = .981, stde = 100, beta=c(0.1, 0.2, 0.3, -0.1)) mcGraph3(dat3$x1, dat3$x2, dat3$y, theta=-10, interaction = TRUE)

Author(s)

Paul E. Johnson pauljohn@ku.edu

  • Maintainer: Paul E. Johnson
  • License: GPL (>= 3.0)
  • Last published: 2022-08-06

Useful links