source("http://www.statpower.net/R2101/GeometrySupport.R")
## Two linearly independent vectors
setup.3d(6)
x1 <- c(3,4,2)
x2 <- c(2,1,-2)
draw.x1()
draw.x2()
## They form a basis for a two-dimensional subspace (a plane)
## in 3-dimensional Euclidean space
make.plane(x1,x2)
## The sum of two vectors terminates at the vertex of a parallelogram
draw.sum(x1,x2)
complete.parallelogram(x1,x2)
## Mean and Deviation score via Projection
setup.3d(6)
x1 <- c(4,-1,3)
draw.arrow.3d(x1,color="red") ## draw x1
draw.one() ## draw the unit vector
## We compute the mean vector by projecting onto the unit vector
## Notice, it is exactly 2 times as long as the unit vector
mean.vector <- project.y.onto.x(x1,UnitVector(3))
## Compute deviation score vector and draw it
deviation.score.vector <- x1-mean.vector
draw.arrow.3d(deviation.score.vector,color="green")
## This vector can be viewed as a projection into the orthogonal complement of
## The unit vector, i.e., "Deviation Score Space"
## In 3 dimensions, this space is 2 dimensional, and in general
## we speak of n-1 dimensional deviation score space
## Any 2 linearly independent vectors can serve as a basis
## With the next two commands, we draw deviation score space
basis <- Q(UnitVector(3)) %*% matrix(rnorm(6),3,2)
make.plane(as.vector(basis[,1]),as.vector(basis[,2]))
## Correlation as a cosine IN DEVIATION SCORE SPACE
setup.3d(6)
x1 <- c(3,4,2)
x2 <- c(2,1,-2)
draw.x1()
draw.x2()
cor(x1,x2)
## x1 and x2 are almost at right angles!
## Now, project them into deviation score space
Q1 <- Q(UnitVector(3))
x1 <- as.vector(Q1 %*% x1)
x2 <- as.vector(Q1 %*% x2)
draw.x1()
draw.x2()
## Now the angle is close to 45 degrees
## projection into a plane
setup.3d(6)
x1 <- c(4,-1,3)
x2 <- c(1,3,5)
y <- .3*x1 +.2*x2 + 1.3
draw.x1()
draw.x2()
make.plane(x1,x2)
draw.y()
draw.yhat()
draw.e()
## bivariate linear regression with an intercept
setup.3d(6)
x1 <- c(4,-1,3)
x2 <- c(1,1,1)
y <- c(4,2,2)
draw.x1()
draw.x2()
make.plane(x1,x2)
draw.y()
draw.yhat()
draw.e()
# Multivariate Linear regression with deviation score variables
Q1 <- Q(UnitVector(3))
x1 <- c(4,-1,3)
x2 <- c(1,3,5)
y <- c(4,2,2)
x1 <- as.vector(Q1 %*% x1)
x2 <- as.vector(Q1 %*% x2)
setup.3d(6)
draw.x1()
draw.x2()
make.plane(x1,x2)
draw.y()
draw.yhat()
draw.e()