IIMT 2641 Introduction to Business Analytics
Module 3: Linear Regression - Lab
1
Create an R script
2
R script
§ A text file containing a set of commands and comments
– Put your comments after the # sign.
§ Why to use R script? Instead of re-entering codes each time executing a
set of commands, . . .
– Reproducibility
– Anyone anywhere with the data and R script can produce the results.
– Big time savings when repeating analysis of similar data
3
Specify a working directory in R
§ Working directory: the default location where R will look for files you
want to load and where it will put any files you save
§ Use the function setwd() to change the working directory
setwd("/Users/fengtian/Dropbox/HKU/Intro to BA/Intro to BA 25 -
26/Module 3 Linear Regression/Codes and Data")
§ Use the function getwd() to display the current working directory.
getwd()
4
Loading data from working directory
§ Dataset [Link]
§ For CSV files:
– Wine <- [Link]("[Link]")
§ For RData files:
– Wine <- load("[Link]")
5
Data frames
§ A data frame is the data structure (we can think of it like an Excel
spreadsheet). Useful functions for data frames include:
– str(): examine structure of the object
– names(): return a vector of variable names
– nrow(): return the number of rows
– ncol(): return the number of columns
– dim(): combine ncol() and nrow() into a vector
– summary(): provide a statistical summary
– head(): displays the first six observations
– tail(): displays the last six observations
– View(): displays the spreadsheet of the entire data frame
§ Load [Link], assign it to an object called Wine, and try the above
functions on this newly created data frame.
6
Data frames
§ A data frame is the data structure (we can think of it like an Excel
spreadsheet). Useful functions for data frames include:
– str(): examine structure of the object
– names(): return a vector of variable names
7
Data frames
§ A data frame is the data structure (we can think of it like an Excel
spreadsheet). Useful functions for data frames include:
– nrow(): return the number of rows
– ncol(): return the number of columns
– dim(): combine ncol() and nrow() into a vector
8
Data frames
§ A data frame is the data structure (we can think of it like an Excel
spreadsheet). Useful functions for data frames include:
– summary(): provide a statistical summary
9
Data frames
§ A data frame is the data structure (we can think of it like an Excel
spreadsheet). Useful functions for data frames include:
– head(): displays the first six observations
– tail(): displays the last six observations
– View(): displays the spreadsheet of the entire data frame
10
Data frames: using []
§ We can retrieve specified observations and variables using brackets [ ] with
a comma in the form [rows, columns]:
Wine[1:3, "Age"]
[1] 31 30 28
Wine[1:3, 1]
[1] 1952 1953 1955
Observe that “Year” is the first variable in the “Wine” data frame.
11
Data frames: using $
§ The $ operator is another way to access variables from a data frame:
head(Wine$Age, 5)
[1] 31 30 28 26 25
§ Note: the “5” after the comma specifies how many observations to display.
12
Save R script
13
Save objects
§ When you quit RStudio, you will be asked whether you would like to save
the workspace.
§ You should answer no in general: we only want to save what we want!
§ To export CSV:
[Link](Wine, file = "[Link]")
§ To export RData:
save(Wine, file = "[Link]")
14
Estimate a linear model: lm()
§ Fit a regression line (we save the model to WineRegOne)
§ We do not need to use $ to refer to variables here, because we have the data
argument telling R which data set to use.
WineRegOne <- lm(LogPrice ~ AGST, data=Wine)
§ Check the output of the model:
summary(WineRegOne)
15
One-Variable Linear Regression (Add best fit line to plot)
plot(Wine$AGST, Wine$LogPrice, main="Scatterplot of Price vs Average
growing season temperature")
abline(WineRegOne, col="red") # plot the regression line
16
! -3.4178 + 0.6351*AGST
𝑦=
How well the model fits data
WineRegOne$residuals #residuals of the model
SSE = sum(WineRegOne$residuals^2) #residuals of the model
# calculate SST & R-squared
ybar = mean(Wine$LogPrice)
resi = Wine$LogPrice - ybar
SST = sum(resi^2)
SSE = 5.73
SST = 10.15
17 R! = 1 − SSE/SST = 0.435
Estimate a linear model:
Two Variables
WineRegTwo <- lm(LogPrice ~ AGST + HarvestRain, data=Wine) # linear
regression model2
summary(WineRegTwo)
SSE = sum(WineRegTwo$residuals^2)
18 SSE = 2.97 < SSE1 = 5.73
Estimate a linear model (All Variables)
WineReg <- lm(LogPrice ~ WinterRain + AGST + HarvestRain + Age ,
data=Wine) # linear regression model
summary(WineReg) # Check the regression results
19
SSE = 1.73
Example of Correlation
Correlation between Harvest rain and Average growing season temperature
= -0.0645
cor(Wine$HarvestRain, Wine$AGST)
20
Example of Correlation
Correlation between Age of wine (years) and Population of France (in
thousands) = -0.9945
cor(Wine$Age, Wine$FrancePop)
21
Example of Correlation
cor(Wine)
22
VIF
[Link]("car")
library(car) #load the car library
vif(WineReg) #calculate the VIF for each predictor variable in the model
23
Estimate a linear model (Re-run the model by leaving out
FrancePop)
WineRegFour <- lm(LogPrice ~ WinterRain + AGST +
HarvestRain + Age , data=Wine) # linear regression model
summary(WineRegFour) # Check the regression results
24
Make predictions
We can make predictions on new observations by using predict.
WineTest <- [Link]("[Link]")
WinePredictions <- predict(WineRegFour, newdata=WineTest)
str(WinePredictions)
25
Compare to the actual values
Out-of-sample R!
SSE <- sum((WineTest$Price - WinePredictions)^2)
SST <- sum((WineTest$Price - mean(Wine$Price))^2)
1 - SSE/SST
Use the mean of Price in the training set to calculate SST.
26