Latent variable methods and applications (2012)

From Statistics for Engineering
Revision as of 18:29, 21 September 2018 by Kevin Dunn (talk | contribs)
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
Class date(s): 29 March 2012
Download video: Link (plays in Google Chrome) [465 Mb]

Course notes

This section will be briefly covered in class on 29 March 2012. Only the basic concepts will be covered.

  • Course notes. Please read the start of Chapter 6 for an overview. The rest of the chapter will not be covered in this course.


Projector overheads

A high-level overview of latent variable methods


Code that shows least squares failing

We get very different models for very small changes in the raw data when our variables are strongly correlated.

# Create data with strongly correlated X's
set.seed(0)
x1 <- rnorm(10)
x2 <- x1 * 2 + rnorm(10, sd=0.01)
x3 <- rnorm(10)
y  <- 3*x1 + 4*x2 - 5*x3 + rnorm(10, sd=0.05)
plot(data.frame(x1, x2, x3, y))
mod.corr <- lm(y ~ x1 + x2 + x3)
summary(mod.corr)
confint(mod.corr)

# Coefficients:
# (Intercept)           x1           x2           x3  
#     0.02144     -0.95069      5.96684     -5.00735


# Change one of the entries in x1: before
# ----------------------------------------
#  [1]  1.262954285 -0.326233361  1.329799263  1.272429321  0.414641434
#  [6] -1.539950042 -0.928567035 -0.294720447 -0.005767173  2.404653389

x1[1] = 1.25
# After:
# ------
# [1]  1.250000000 -0.326233361  1.329799263  1.272429321  0.414641434
# [6] -1.539950042 -0.928567035 -0.294720447 -0.005767173  2.404653389


mod.updated <- lm(y ~ x1 + x2 + x3)
summary(mod.updated)
confint(mod.updated)

# Coefficients:
# (Intercept)           x1           x2           x3  
#     0.01978      0.51850      5.23192     -5.00882

# The coefficient for x1 has flipped sign; 
# R2 is 1.0 in both cases!
# S_E = 0.03323 in both cases!

Code for the rotating cube

Created using R with this code, then converted to a video file using http://ffmpeg.org/

temps<- read.csv('http://openmv.net/file/room-temperature.csv')
summary(temps)
X <- data.frame(x1=temps$FrontLeft, x2=temps$FrontRight, x3=temps$BackLeft)

# To colour-code sub-groups of outliers
library(lattice)
grouper = c(numeric(length=50)+1, numeric(length=10)+2, numeric(length=144-50-10)+1)
grouper[131] = 3

cube <- function(angle){
    # Function to draw the cube at a certain viewing ``angle``
    xlabels = 0
    ylabels = 0
    zlabels = 0
    lattice.options(panel.error=NULL) 
    print(cloud(
                 X$x3 ~ X$x1 * X$x2,
                 cex = 1, 
                 type="p",
                 groups = grouper,
                 pch=20,
                 col=c("black", "blue", "blue"),   
                 main="",
                 screen = list(z = angle, x = -70, y = 0),        
                 par.settings = list(axis.line = list(col = "transparent")), 
                 scales = list(
                     col = "black", arrows=TRUE,
                     distance=c(0.5,0.5,0.5)
                 ),
                 xlab="x1",
                 ylab="x2",
                 zlab="x3",
                 zoom = 1.0
             )
        )  
}

angles = seq(0, 360, 1)
for(i in angles){
    
    if (i<10) {
        filename <- paste("00", as.character(i), ".jpg", sep="")
    } else if (i<100) {
        filename <- paste("0", as.character(i), ".jpg", sep="")
    } else {
        filename <- paste("", as.character(i), ".jpg", sep="")
    }
        
    jpeg(file=filename, height = 1000, width = 1000, quality=100, res=300)
    cube(i)
    dev.off()
}

# Install "ffmpeg" to stitch the JPG files into an animation
system("ffmpeg -r 20 -b 1800 -i %03d.jpg animated-spinning-cube.mp4")