Skip to content

Instantly share code, notes, and snippets.

@rpietro
Last active August 29, 2015 14:08
Show Gist options
  • Save rpietro/7db2204d618f8b4395ad to your computer and use it in GitHub Desktop.
Save rpietro/7db2204d618f8b4395ad to your computer and use it in GitHub Desktop.
code for Appendix: A Review of Matrices
# Script for Appendix E: A Review of Matrices @ http://www.personality-project.org/r/book/AppendixE.F.pdf
(v1 <- seq(1, 6))
(v2 <- seq(7, 12))
(v3 <- v1 + 20)
(v4 <- v1 + v2)
(v5 <- v1 * 3)
(v6 <- v1 * v2) # multiplies element by element, not a matrix multiplication
(v7 <- seq(1,4))
(V8 <- v1%*%t(v7)) #transpose to get the inner product
(V9<- v7%*%t(v1))
v1 %*% t(v7)
v7 %*% t(v1)
(v <- seq(1, 7))
(one <- rep(1,length(v)))
(sum.v <- t(one) %*% v) #transpose of a unit vector times a vector v = sum elements of v
(mean.v <- sum.v * (1/length(v)))
(mean.v <- t(one) %*% v * (1/length(v)))
(mean.v <- t(one) %*% v/length(v))
sum.v * (1/length(v))
t(one) %*% v * (1/length(v))
t(one) %*% v/length(v)
v - mean.v # difference vector
t(v - mean.v) %*% (v - mean.v) # sum of square difference obtained by multiplying the transpose of the difference vector by the difference vector
(Var.v <- t(v - mean.v) %*% (v - mean.v) * (1/(length(v) - 1)))
scale(v, scale = FALSE)
mean(v)
var(v)
(x <- c(v1, v2,v3))
(Xc <- cbind(v1, v2, v3))
(Xr <- rbind(v1, v2, v3))
dim(Xc)
dim(Xr)
(Xij <- matrix(seq(1:24), ncol = 4))
(rownames(Xij) <- paste("S", seq(1, dim(Xij)[1]), sep = ""))
(colnames(Xij) <- paste("V", seq(1, dim(Xij)[2]), sep = ""))
Xij
t(Xij)
Xij + 4
round((Xij + 4)/3, 2)
(v <- 1:4)
Xij
Xij + v # sum a matrix to an nn i1 matrix is done in column sequence
Xij * v # multiplication of a matrix by an i1 matrix is done in column sequence, notice that this is not a matrix multiplication
# stopped here
t(t(Xij) + v)
(V10 <- t(t(Xij) * v))
V10
scale(V10,scale=FALSE)
scale(V10,scale=FALSE)
dim(Xij)
X.means <- t(one) %*% Xij /dim(Xij)[1] #find the column average
colMeans(Xij)
X.diff <- Xij - one %*% X.means
X.diff
set.seed(42) #set random seed for a repeatable example
Xij <- matrix(sample(Xij),ncol=4) #random sample from Xij
rownames(Xij) <- paste("S", seq(1, dim(Xij)[1]), sep = "")
colnames(Xij) <- paste("V", seq(1, dim(Xij)[2]), sep = "")
Xij
X.means <- t(one) %*% Xij /dim(Xij)[1] #find the column average
X.diff <- Xij -one %*% X.means
X.diff
X.cen <- scale(Xij,scale=FALSE)
X.cov <- t(X.diff) %*% X.diff /(dim(X.diff)[1]-1)
round(X.cov)
round(cov(Xij),2)
pVxVy .
X.var <- diag(X.cov)
sdi <- diag(1/sqrt(diag(X.cov)))
rownames(sdi) <- colnames(sdi) <- colnames(X.cov)
round(sdi, 2)
X.cor <- sdi %*% X.cov %*% sdi #pre and post multiply by 1/sd
rownames(X.cor) <- colnames(X.cor) <- colnames(X.cov)
round(X.cor, 2)
round(cor(Xij), 2)
I <- diag(1,nrow=dim(X.cov)[1])
X.inv <- solve(X.cov)
round(X.cov %*% X.inv, 2)
round(X.inv %*% X.cov, 2)
datafilename = "http://personality-project.org/R/datasets/extraversion.items.txt"
items = read.table(datafilename, header = TRUE)
items <- items[, -1]
dim(items)
library(psych)
describe(items)
pairs.panels(items)
keys <- matrix(c(1, -1, 1, 0, 0, 0, 0, 0, -1, 1), ncol = 2) #specify
keys # and show the keys matrix
X <- as.matrix(items) #matrix operations require matrices
X.ij <- X %*% keys #this results in the scale scores
n <- dim(X.ij)[1] # how many subjects?
one <- rep(1, dim(X.ij)[1])
X.means <- t(one) %*% X.ij/n
X.cov <- t(X.ij - one %*% X.means) %*% (X.ij - one %*% X.means)/(n - 1)
round(X.cov, 2)
keys
X.sd <- diag(1/sqrt(diag(X.cov)))
X.cor <- t(X.sd) %*% X.cov %*% (X.sd)
round(X.cor, 2)
keys <- matrix(c(1, -1, 1, 0, 0, 0, 0, 0, -1, 1), ncol = 2)
X.cor <- cor(X)
round(X.cor, 2)
X.cov <- t(keys) %*% X.cor %*% keys
X.sd <- diag(1/sqrt(diag(X.cov)))
X.cor <- t(X.sd) %*% X.cov %*% (X.sd)
keys
round(X.cov, 2)
round(X.cor, 2)
datafilename <- "http://personality-project.org/R/datasets/extraversion.items.txt"
items = read.table(datafilename, header = TRUE)
items <- items[, -1]
key <- matrix(c(1, -1, 1, 0, 0, 0, 0, 0, -1, 1, 1, -1, 1, -1, 1), ncol = 3)
colnames(key) <- c("V1-3", "V4-5", "V1-5")
rownames(key) <- colnames(items)
key
raw.r <- cor(items)
V <- t(key) %*% raw.r %*% key
rownames(V) <- colnames(V) <- c("V1-3", "V4-5", "V1-5")
round(V, 2)
n <- diag(t(key) %*% key)
alpha <- (diag(V) - n)/(diag(V)) * (n/(n - 1))
round(alpha, 2)
beta <- solve(t(X) %*% X) %*% (t(X) %*% Y)
round(beta, 2)
lm(Y ~ -1 + X)
one <- rep(1, dim(X)[1])
X <- cbind(one, X)
print(X)
beta <- solve(t(X) %*% X) %*% (t(X) %*% Y)
round(beta, 2)
lm(Y ~ X)
R <- matrix(c(1, 0.56, 0.48, 0.56, 1, 0.42, 0.48, 0.42, 1), ncol = 3)
rxy <- matrix(c(0.4, 0.35, 0.3), ncol = 1)
colnames(R) <- rownames(R) <- c("x1", "x2", "x3")
rownames(rxy) <- c("x1", "x2", "x3")
colnames(rxy) <- "y"
beta <- solve(R, rxy)
round(beta, 2)
C <- matrix(c(3,2,2,4),byrow=TRUE,nrow=2) >C
solve(C)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment