Homework 3

Saša Kočar

Data set 1

This data set was retrieved from Kaggle.com. ANN - Car Sales Price Prediction. (https://www.kaggle.com/datasets/yashpaloswal/ann-car-sales-price-prediction)

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

Description of data

Unit of Observation: Buyer of a car
Sample Size: sample size is 100
ID: A unique identifier for each person that bought car.
Sex: Gender of the buyer of the car. (Male = 0, Female = 1)
Salary: Annual salary of the buyer in $
Price: Price for which the buyer purchased the car in $.
Price of a car = b0 + b2 * Salary + Gender (dummy)
I expect that the higher the persons salary, more expensive will be the car, assuming the other variable stays unchanged.
Given the salary, I expect man to spend more on a car than women.
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

Research Question:

“How does gender and annual salary affect the amount spend on a car?”

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

Interpretation:

Salary: Minimum salary was 37333 and maximum 100000. 50% of the buyers earned up to 65530 and 50% above that.
Price: Minimum amount spent on car was 20653$ and maximum was 70878$.
There was 58 male and 42 female in our sample.
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)

Multicolinearity

vif(fit1)
##    Salary SexFactor 
##  1.004657  1.004657
There are no issues with too strong multicolinearity (VIF<5).
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

H0: Erorrs are normally distributed

H1: Errors are not normally distributed

BSD we can not reject null hypothesis (p = 0,12) and conclude that errors are normally distributed.

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
Here we can also better observe that unit with ID 486 is HIV. Therefore we will remove it in the next step.
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
H0: the variance is constant.
H1: the variance is not constant
BSD we can not reject null hypothesis. (p = 0.56) Homoskedasticity is not violated.
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

•For an additional dollar of salary, the price of the car on average increases by 0,63 dollars. With gender remaining constant. (p>0.001).

• We dont explain gender, p-value statistically insignificant (p>0.05). If it would be statistically significant: Given the salary of the person, female on average spend 1849 dollars less than man.

Coefficient of determination: 48.5% of variability of price can be explained by variability of salary and gender.

F-statistics

H0: beta1=beta2=0

H1: At least one is different than 0.

We reject null hypothesis (p<0.001) and conclude that at least one of the explanatory variables impacts the price of a car.

sqrt(summary(fit1)$r.squared) # Coefficient of multiple correlation
## [1] 0.7164819

Linear relationship between price, salary and gender is strong.

library(lm.beta)
lm.beta(fit1)
## 
## Call:
## lm(formula = Price ~ Salary + SexFactor, data = mydata)
## 
## Standardized Coefficients::
##     (Intercept)          Salary SexFactorFemale 
##              NA      0.71888540     -0.09892042

Variable salary has the largest effect (0.719>0.099).

Conclusion: BSD we found that salary has statistically significant effect on the price of a car bought.