Difference between revisions of "Latent variable methods and applications (2012)"
Jump to navigation
Jump to search
Kevin Dunn (talk | contribs) (Created page with "__NOTOC__{{ClassSidebar | date = 29 March 2012 | vimeoID1 =39553044 | vimeoID2 = | vimeoID3 = | vimeoID4 = | vimeoID5 = | course_notes_PDF = | course_notes_alt = Course notes...") |
Kevin Dunn (talk | contribs) |
||
(4 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
__NOTOC__{{ClassSidebar | __NOTOC__{{ClassSidebar | ||
| date = 29 March 2012 | | date = 29 March 2012 | ||
| vimeoID1 = | | vimeoID1 = | ||
| vimeoID2 = | | vimeoID2 = | ||
| vimeoID3 = | | vimeoID3 = | ||
Line 12: | Line 12: | ||
| assignment_instructions = | | assignment_instructions = | ||
| assignment_solutions = | | assignment_solutions = | ||
| video_download_link_MP4 = http://connectmv.com/media/stats4eng/4C3-2012-11B.mp4 | | video_download_link_MP4 = | ||
| video_download_link2_MP4 = http://connectmv.com/media/stats4eng/4C3-2012-11B.mp4 | |||
| video_download_link3_MP4 = | | video_download_link3_MP4 = | ||
| video_download_link4_MP4 = | | video_download_link4_MP4 = | ||
| video_download_link5_MP4 = | | video_download_link5_MP4 = | ||
| video_download_link_MP4_size = | | video_download_link_MP4_size = | ||
| video_download_link2_MP4_size = | | video_download_link2_MP4_size = | ||
| video_download_link3_MP4_size = | | video_download_link3_MP4_size = | ||
Line 36: | Line 36: | ||
* [https://learnche.org/pid Course notes]. Please read the start of Chapter 6 for an overview. The rest of the chapter will not be covered in this course. | * [https://learnche.org/pid Course notes]. Please read the start of Chapter 6 for an overview. The rest of the chapter will not be covered in this course. | ||
* A more complete 1-semester course on [https:// | * A more complete 1-semester course on [https://learnche.org/latent latent variable methods] was taught at McMaster in 2011. | ||
Line 48: | Line 48: | ||
We get very different models for very small changes in the raw data when our variables are strongly correlated. | We get very different models for very small changes in the raw data when our variables are strongly correlated. | ||
< | <html><div data-datacamp-exercise data-lang="r" data-height="600px"> | ||
<code data-type="sample-code"> | |||
# Create data with strongly correlated X's | # Create data with strongly correlated X's | ||
set.seed(0) | set.seed(0) | ||
Line 57: | Line 58: | ||
plot(data.frame(x1, x2, x3, y)) | plot(data.frame(x1, x2, x3, y)) | ||
mod.corr <- lm(y ~ x1 + x2 + x3) | mod.corr <- lm(y ~ x1 + x2 + x3) | ||
print('Before the change') | |||
summary(mod.corr) | summary(mod.corr) | ||
confint(mod.corr) | confint(mod.corr) | ||
Line 67: | Line 69: | ||
# Change one of the entries in x1: before | # Change one of the entries in x1: before | ||
# ---------------------------------------- | # ---------------------------------------- | ||
# [1] 1.262954285 -0.326233361 1.329799263 1.272429321 0.414641434 | # [1] 1.262954285 -0.326233361 1.329799263 | ||
# [ | # [4] 1.272429321 0.414641434 -1.539950042 | ||
# [7] -0.928567035 -0.294720447 -0.005767173 2.404653389 | |||
x1[1] = 1.25 | x1[1] = 1.25 | ||
# After: | # After: notice by how little we changed it! | ||
# ------ | # ------ | ||
# [1] 1.250000000 -0.326233361 1.329799263 1.272429321 0.414641434 | # [1] 1.250000000 -0.326233361 1.329799263 | ||
# [ | # [4] 1.272429321 0.414641434 -1.539950042 | ||
# [7] -0.928567035 -0.294720447 -0.005767173 2.404653389 | |||
mod.updated <- lm(y ~ x1 + x2 + x3) | mod.updated <- lm(y ~ x1 + x2 + x3) | ||
print('After the change') | |||
summary(mod.updated) | summary(mod.updated) | ||
confint(mod.updated) | confint(mod.updated) | ||
Line 85: | Line 89: | ||
# 0.01978 0.51850 5.23192 -5.00882 | # 0.01978 0.51850 5.23192 -5.00882 | ||
# The coefficient for x1 has flipped sign | # The coefficient for x1 has flipped sign! The slope | ||
# for x2 is also quite different. | |||
# Confirming that least squares is unstable with | |||
# highly correlated data. | |||
# R2 is 1.0 in both cases! | # R2 is 1.0 in both cases! | ||
# S_E = 0.03323 in both cases! | # S_E = 0.03323 in both cases! | ||
</ | </code> | ||
</div></html> | |||
== Code for the rotating cube == | == Code for the rotating cube == |
Latest revision as of 21:06, 14 January 2019
Class date(s): | 29 March 2012 |
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.
- A more complete 1-semester course on latent variable methods was taught at McMaster in 2011.
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)
print('Before the change')
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
# [4] 1.272429321 0.414641434 -1.539950042
# [7] -0.928567035 -0.294720447 -0.005767173 2.404653389
x1[1] = 1.25
# After: notice by how little we changed it!
# ------
# [1] 1.250000000 -0.326233361 1.329799263
# [4] 1.272429321 0.414641434 -1.539950042
# [7] -0.928567035 -0.294720447 -0.005767173 2.404653389
mod.updated <- lm(y ~ x1 + x2 + x3)
print('After the change')
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! The slope
# for x2 is also quite different.
# Confirming that least squares is unstable with
# highly correlated data.
# 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")