Download presentation
Presentation is loading. Please wait.
1
S-PLUS Lecture 3 Jaeyong Lee
2
Factors A factor and a category are special types of vector, normally used to hold a categorical variable in many statistical functions. Category is deprecated. Factor has a class attribute, hence it is adapted to generic function mechanism.
3
Session: Factors citizen <- c(“uk”, “us”, “no”,”au”,”uk”,”us”, “us”,”no”,”au”) citizenf <- factor(citizen) attributes(citizenf) unclass(citizenf) # This is the same as category(citizen) table(citizenf) citizeno <- ordered(citizen, levels=“us”,”au”,”no”,”uk”) ordered(cut(geyser$duration, breaks=0:6),levels=1:6) income <- c(10, 20, 15, 12, 17, 13, 22, 9,14) tapply(income, citizenf, mean)
4
Arrays A matrix is a two dimensional collection of data and an array is a generalization of matrix. A dimension vector of an array is an attribute of the array representing the dimension. S arrays use column-major order: the first index moves fastest, and the last slowest.
5
Session: Arrays 1 z <- 1:150 a <- array(z,dim=c(3,5,10))
dim(z) <- c(3,5,10) z[1,1,1] z[2,1,1] matrix(1:6,nrow=2,ncol=3) z <- matrix(1:6,nrow=2, ncol=3, byrow=T) z[1,] z[,2]
6
Session: Arrays 2 x <- matrix(0,nc=5,nr=5); x
i <- matrix(1:5,5,2); i x[i] <- 1; x matrix(1:6, 3, 2)*matrix(1:6*2, 3, 2) X <- matrix(1:6,3,2) y <- 1:3 t(X) %*% y
7
Session: Array 3 A <- matrix(c(1,1,2,3),2,2) b <- c(2,5)
solve(A,b) diag(rep(1,2)) solve(A,diag(rep(1,2))) A[2, ] <- c(2,7) chol(A) t(chol(A)) %*% chol(A)
8
Session: Array 4 eg <- eigen(A) eg$values eg$vectors
t(eg$vectors) %*% diag(eg$values) %*% eg$vectors X <- matrix(1:6,3,2) s <- svd(X) s s$u %*% diag(s$d) %*% t(s$v)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.