data <- read.csv("car_purchasing.csv")
data <- cbind(ID = 1:nrow(data), data)
mydata <- data[, -c(2, 3, 4, 6, 8, 9)]
library(psych)
colnames(mydata) <- c("ID","Sex","Salary", "Price")
# Sampling 100 units
set.seed(1) # Setting initial point of sampling
mydata <- mydata[sample(nrow(mydata), 100), ]
head(mydata, 10)
## ID Sex Salary Price
## 324 324 0 49607.23 22630.26
## 167 167 1 52477.66 20653.21
## 129 129 1 83626.31 52665.37
## 418 418 0 64412.43 39439.45
## 471 471 0 81565.96 69669.47
## 299 299 0 55369.73 29670.83
## 270 270 0 70914.60 66888.94
## 466 466 0 92455.73 61404.23
## 187 187 0 42415.49 39522.13
## 307 307 0 74418.56 56071.61
mydata$SexFactor <- factor(mydata$Sex,
levels = c(0, 1),
labels = c("Male", "Female"))
library(psych)
describeBy(mydata$Price, mydata$SexFactor)
##
## Descriptive statistics by group
## group: Male
## vars n mean sd median trimmed mad min max
## X1 1 58 45144.24 11540.02 43404.1 44796.89 11095.81 22630.26 70878.3
## range skew kurtosis se
## X1 48248.04 0.27 -0.54 1515.28
## ------------------------------------------------------------
## group: Female
## vars n mean sd median trimmed mad min max
## X1 1 42 44440.14 12647.94 44222.83 44183.26 14946.32 20653.21 68678.44
## range skew kurtosis se
## X1 48025.22 0.15 -0.97 1951.62
summary(mydata[ , c( -1, -2)])
## Salary Price SexFactor
## Min. : 37336 Min. :20653 Male :58
## 1st Qu.: 55369 1st Qu.:35890 Female:42
## Median : 65530 Median :43502
## Mean : 64518 Mean :44849
## 3rd Qu.: 72343 3rd Qu.:53536
## Max. :100000 Max. :70878
library(car)
## Loading required package: carData
##
## Attaching package: 'car'
## The following object is masked from 'package:psych':
##
## logit
scatterplotMatrix(mydata[ , c( 4, 3)],
smooth = FALSE)
#### From the scatterplot we can observe that the relationship between
price and salary is linear.
scatterplot(Price ~ Salary,
ylab = "Price of car",
xlab = "Salary",
smooth = FALSE,
boxplot = FALSE,
data = mydata)
#Estimating regression function
fit1 <- lm(Price ~ Salary + SexFactor,
data = mydata)
vif(fit1)
## Salary SexFactor
## 1.004657 1.004657
mydata$StdResid <- round(rstandard(fit1), 3)
mydata$CooksD <- round(cooks.distance(fit1), 3)
#Checking histograms
hist(mydata$StdResid,
xlab = "Standardised residuals",
ylab = "Frequency",
main = "Histogram of standardised residuals")
#### With the histogram we check if all values fall into the interval of
+ - 3. Since this criteria is met, we conclude that we do not have any
outliers.
#Checking for normality
shapiro.test(mydata$StdResid)
##
## Shapiro-Wilk normality test
##
## data: mydata$StdResid
## W = 0.97933, p-value = 0.1179
hist(mydata$CooksD,
xlab = "Cooks distance",
ylab = "Frequency",
main = "Histogram of Cooks distance")
#### With Cooks distances we check for HIV. None of the values should be
above the value 1. In this case this is fullfiled. However we also have
to check for gaps. We can observe that there is a big gap between 0.04
and 0.12.
head(mydata[order(-mydata$CooksD), c("ID", "CooksD")], 6)
## ID CooksD
## 486 486 0.122
## 167 167 0.039
## 45 45 0.039
## 304 304 0.037
## 375 375 0.033
## 252 252 0.031
library(dplyr)
##
## Attaching package: 'dplyr'
## The following object is masked from 'package:car':
##
## recode
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
mydata <- mydata %>%
filter (!ID %in% c(486)) #Removing ID486
hist(mydata$CooksD,
xlab = "Cooks distance",
ylab = "Frequency",
main = "Histogram of Cooks distance")
#### We check again, now we do not have any gaps.
fit1 <- lm(Price ~ Salary + SexFactor, data = mydata)
mydata$StdFitted <- scale(fit1$fitted.values)
library(car)
scatterplot(x = mydata$StdFitted, y = fit1$residuals,
ylab = "Standardized Residuals",
xlab = "Standardized Fitted Values",
boxplots = FALSE,
smooth = FALSE)
#### We can observe that homoskedasticity and linearity are fulfilled in
the graph.
#checking homoskedasticity with BP test
library(olsrr)
##
## Attaching package: 'olsrr'
## The following object is masked from 'package:datasets':
##
## rivers
ols_test_breusch_pagan(fit1)
##
## Breusch Pagan Test for Heteroskedasticity
## -----------------------------------------
## Ho: the variance is constant
## Ha: the variance is not constant
##
## Data
## ---------------------------------
## Response : Price
## Variables: fitted values of Price
##
## Test Summary
## ----------------------------
## DF = 1
## Chi2 = 0.3534676
## Prob > Chi2 = 0.5521568
summary(fit1)
##
## Call:
## lm(formula = Price ~ Salary + SexFactor, data = mydata)
##
## Residuals:
## Min 1Q Median 3Q Max
## -19723.8 -6133.2 -264.6 6141.3 17689.2
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2905.2526 4346.3521 0.668 0.505
## Salary 0.6625 0.0659 10.053 <2e-16 ***
## SexFactorFemale -2401.3229 1735.9115 -1.383 0.170
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 8471 on 96 degrees of freedom
## Multiple R-squared: 0.5133, Adjusted R-squared: 0.5032
## F-statistic: 50.63 on 2 and 96 DF, p-value: 9.695e-16
sqrt(summary(fit1)$r.squared) # Coefficient of multiple correlation
## [1] 0.7164819
library(lm.beta)
lm.beta(fit1)
##
## Call:
## lm(formula = Price ~ Salary + SexFactor, data = mydata)
##
## Standardized Coefficients::
## (Intercept) Salary SexFactorFemale
## NA 0.71888540 -0.09892042