Difference between revisions of "Least squares modelling (2013)"

From Statistics for Engineering
Jump to navigation Jump to search
 
(10 intermediate revisions by the same user not shown)
Line 1: Line 1:
__NOTOC__{{ClassSidebar
__NOTOC__{{ClassSidebar
| date = 08 February 2013
| date = 08 February 2013 to 07 March 2013
| dates_alt_text =  
| dates_alt_text =  
| vimeoID1 = 59609654
| vimeoID1 = 59609654
| vimeoID2 = 59763486
| vimeoID2 = 59763486
| vimeoID3 = 60592349
| vimeoID3 = 60592349
| vimeoID4 =
| vimeoID4 = 60688909
| vimeoID5 =
| vimeoID5 = 60862565
| vimeoID6 =
| vimeoID6 = 61138805
| vimeoID7 =
| vimeoID7 = 61211690
| vimeoID8 =
| vimeoID8 =
| course_notes_PDF = 2013-4C3-Overheads-Least-squares-modelling.pdf
| course_notes_PDF = 2013-4C3-Overheads-Least-squares-modelling.pdf
Line 25: Line 25:
| video_notes3 =
| video_notes3 =
| video_download_link4_MP4 = http://learnche.mcmaster.ca/media/4C3-2013-Class-07B.mp4
| video_download_link4_MP4 = http://learnche.mcmaster.ca/media/4C3-2013-Class-07B.mp4
| video_download_link4_MP4_size = M
| video_download_link4_MP4_size = 290 M
| video_notes4 =
| video_notes4 =
| video_download_link5_MP4 =
| video_download_link5_MP4 =http://learnche.mcmaster.ca/media/4C3-2013-Class-07C.mp4
| video_download_link5_MP4_size =  
| video_download_link5_MP4_size = 375 M
| video_notes5 =
| video_notes5 =
| video_download_link6_MP4 =  
| video_download_link6_MP4 = http://learnche.mcmaster.ca/media/4C3-2013-Class-08A.mp4
| video_download_link6_MP4_size = M
| video_download_link6_MP4_size = 372 M
| video_notes6 =
| video_notes6 =
| video_download_link7_MP4 =  
| video_download_link7_MP4 = http://learnche.mcmaster.ca/media/4C3-2013-Class-08B.mp4
| video_download_link7_MP4_size = M
| video_download_link7_MP4_size = 380 M
| video_notes7 =
| video_notes7 =
| video_download_link8_MP4 =
| video_download_link8_MP4 =
Line 40: Line 40:
| video_notes8 =
| video_notes8 =
}}
}}
<span style="color:#900000">{{Huge|This page is out of date.}}</span> {{Huge|Please see the [[Least_squares_modelling |latest version of these notes]].}}


== Course notes and slides ==
== Course notes and slides ==
Line 53: Line 57:


== Code used in class ==
== Code used in class ==
Least squares demo
<syntaxhighlight lang="sas">
x <- c(10, 8, 13, 9, 11, 14, 6, 4, 12, 7, 5)
y <- c(8.04, 6.95, 7.58, 8.81, 8.33, 9.96, 7.24, 4.26, 10.84, 4.82, 5.68)
plot(x,y)
model.ls <- lm(y ~ x)
summary(model.ls)
coef(model.ls)
confint(model.ls)
names(model.ls)
model.ls$resisduals
resid(model.ls)
plot(x, y)
abline(model.ls)
</syntaxhighlight>


Thermocouple data
Thermocouple data
<syntaxhighlight lang="sas">
<syntaxhighlight lang="sas">
V <- c(0.01, 0.12, 0.24, 0.38, 0.51, 0.67, 0.84, 1.01, 1.15, 1.31)
V <- c(0.01, 0.12, 0.24, 0.38, 0.51, 0.67, 0.84, 1.01, 1.15, 1.31)
Line 80: Line 103:
plot(V, T)
plot(V, T)
abline(model, col="red")
abline(model, col="red")
</syntaxhighlight>
Multiple linear regression (manually and automatically with R)
<syntaxhighlight lang="sas">
# Calculate the model manually
x1.raw <- c(1,3,4,7,9,9)
x2.raw <- c(9,9,6,3,1,2)
y.raw  <- c(3,5,6,8,7,10)
n <- length(x1.raw)
x1 <- x1.raw - mean(x1.raw)
x2 <- x2.raw - mean(x2.raw)
y <- y.raw - mean(y.raw)
X <- cbind(x1, x2)
# Calculate:  b = inv(X'X) X'y
XTX <- t(X) %*% X    # compare this to cov(X)*(n-1)
XTY <- t(X) %*% y
XTX.inv <- solve(XTX)
b <- XTX.inv %*% XTY
b
# Let R calculate the model:
model <- lm(y.raw ~ x1.raw + x2.raw)
summary(model)
</syntaxhighlight>
</syntaxhighlight>

Latest revision as of 07:18, 4 January 2017

Class date(s): 08 February 2013 to 07 March 2013
Nuvola mimetypes pdf.png (PDF) Course slides
Download video: Link (plays in Google Chrome) [352 M]

Download video: Link (plays in Google Chrome) [347 M]

Download video: Link (plays in Google Chrome) [352 M]

Download video: Link (plays in Google Chrome) [290 M]

Download video: Link (plays in Google Chrome) [375 M]

Download video: Link (plays in Google Chrome) [372 M]

Download video: Link (plays in Google Chrome) [380 M]


This page is out of date. Please see the latest version of these notes.


Course notes and slides


Software source code

Take a look at the software tutorial.


Code used in class

Least squares demo

x <- c(10, 8, 13, 9, 11, 14, 6, 4, 12, 7, 5)
y <- c(8.04, 6.95, 7.58, 8.81, 8.33, 9.96, 7.24, 4.26, 10.84, 4.82, 5.68)
plot(x,y)
model.ls <- lm(y ~ x)
summary(model.ls)

coef(model.ls)
confint(model.ls)

names(model.ls)
model.ls$resisduals
resid(model.ls)

plot(x, y)
abline(model.ls)


Thermocouple data

V <- c(0.01, 0.12, 0.24, 0.38, 0.51, 0.67, 0.84, 1.01, 1.15, 1.31)
T <- c(273, 293, 313, 333, 353, 373, 393, 413, 433, 453)
plot(V, T)
model <- lm(T ~ V)
summary(model)
coef(model)
confint(model)   # get the coefficient confidence intervals
resid(model)     # model residuals
library(car)
qqPlot(resid(model)) # q-q plot of the residuals to check normality

plot(V, T)
v.new <- seq(0, 1.5, 0.1)
t.pred <- coef(model)[1] + coef(model)[2] * v.new
lines(v.new, t.pred, type="l", col="blue")

# Plot x against the residuals to check for non-linearity
plot(V, resid(model))
abline(h=0)

# Plot the raw data and the regression line in red
plot(V, T)
abline(model, col="red")

Multiple linear regression (manually and automatically with R)

# Calculate the model manually
x1.raw <- c(1,3,4,7,9,9)
x2.raw <- c(9,9,6,3,1,2)
y.raw  <- c(3,5,6,8,7,10)
n <- length(x1.raw)

x1 <- x1.raw - mean(x1.raw)
x2 <- x2.raw - mean(x2.raw)
y <- y.raw - mean(y.raw)

X <- cbind(x1, x2)

# Calculate:  b = inv(X'X) X'y
XTX <- t(X) %*% X    # compare this to cov(X)*(n-1)
XTY <- t(X) %*% y
XTX.inv <- solve(XTX)
b <- XTX.inv %*% XTY
b

# Let R calculate the model:
model <- lm(y.raw ~ x1.raw + x2.raw)
summary(model)