Hi all! Here are my Workshop 5 codes.

Download Triangle.csv here

Question 1

tri <- read.csv("Triangle.csv")
str(tri)
## 'data.frame':    14 obs. of  3 variables:
##  $ Store        : int  1 2 3 4 5 6 7 8 9 10 ...
##  $ AvgDispIncome: num  22.3 36.6 55.5 46.7 32.4 31.7 41.6 21.4 44.4 34.1 ...
##  $ Sales        : num  3.7 3.9 6.7 9.5 3.4 5.6 3.7 2.7 5.5 2.9 ...
attach(tri)

# 1a. 
plot(AvgDispIncome, Sales, 
     main = "Graph of Sales '$M against Average Disposable Income '$000")
fit <- lm(Sales~AvgDispIncome)
abline(fit, col = "red")

# 1b. 
cor(AvgDispIncome, Sales)
## [1] 0.6982346
# 1c.
fit
## 
## Call:
## lm(formula = Sales ~ AvgDispIncome)
## 
## Coefficients:
##   (Intercept)  AvgDispIncome  
##       -1.9412         0.1929
# 1d.
summary(fit)$r.squared
## [1] 0.4875316
# 1e.
# residual analysis
par(mfcol = c(2,2))
plot(fit)

par(mfcol = c(1,1))
# 1f.
summary(fit)$coefficients[2,4]
## [1] 0.005480622

Question 2

summary(fit)$r.squared
## [1] 0.4875316
detach(tri)

# Explain Question 3 & 4 with logics

Return to contents page