# # Edps 584/Soc 584/Psych 594 # Spring 2017 # C.J.Anderson # # Function that computes sample statistics in one function. This implements Useful Matrix Formula # from the lecture notes # # Data are psychological test scores on 4 tests and gender of examinees # # # The data: psysc <- read.table(file="C:/Users/cja/Documents/Dropbox/edps 584/Lectures/2Linear Algebra/scores_data.txt", header=TRUE) head(psysc) # The function takes a (n x p) data matrix stats <- function(X){ # Define the function called "stats" if (class(X) != "matrix") { X <- as.matrix(X) } # Check to make sure X is matrix. If not matirx, make it one p <- ncol(X) # Number of variables n <- nrow(X) # Number of cases # Error check if (n<= p) { print("Error: sample size <= number of variables") return } one <- as.matrix(rep(1,n)) # (n x 1) vector of 1's xbar <- t(X) %*% one / n # (p x 1) vector of means dev <- X - one %*% t(one) %*% X / n # (n x p) matrix of deviations from mean In <- diag(1,nrow=n) # (n x n) identity matrix S <- t(X) %*% (In - one %*% t(one) / n ) %*% X/(n-1) # (p x p) covariance matrix W <- (n-1) * S # (p x p) within sums of square and cross-products D <- diag(sqrt((diag(S)))) # (p x p) matirx of standard deviations Di <- solve(D) # (p x p) matirx of inverses of sd's R <- Di %*% S %*% Di # (p x p) correlation matrix return(list(n=n, p=p, xbar=xbar, S=S, W=W, D=D, Di=Di, R=R)) # Output these things } sample.stats <- stats(psysc[,2:5]) # Use of the function sample.stats # Show everything sample.stats$xbar # Just show xbar