In this part of the homework, I’m going to do the Hypothesis testing. The research question, that I’m answering is: Is there a difference in a average amount of money that consumers would spend, between males and females?
mydata <- read.table("./car_purchasing.csv", header=TRUE, fill = TRUE, sep=",", dec=".")
colnames(mydata) <- c("name", "e-mail", "country", "gender", "age", "annual_salary", "credit_card_depth", "net_worth", "car_purchase_amount") #rename variables
head(mydata)
## name e-mail
## 1 Martina Avila cubilia.Curae.Phasellus@quisaccumsanconvallis.edu
## 2 Harlan Barnes eu.dolor@diam.co.uk
## 3 Naomi Rodriquez vulputate.mauris.sagittis@ametconsectetueradipiscing.co.uk
## 4 Jade Cunningham malesuada@dignissim.com
## 5 Cedric Leach felis.ullamcorper.viverra@egetmollislectus.net
## 6 Carla Hester mi@Aliquamerat.edu
## country gender age annual_salary credit_card_depth net_worth
## 1 Bulgaria 0 41.85172 62812.09 11609.381 238961.3
## 2 Belize 0 40.87062 66646.89 9572.957 530973.9
## 3 Algeria 1 43.15290 53798.55 11160.355 638467.2
## 4 Cook Islands 1 58.27137 79370.04 14426.165 548599.1
## 5 Brazil 1 57.31375 59729.15 5358.712 560304.1
## 6 Liberia 1 56.82489 68499.85 14179.472 428485.4
## car_purchase_amount
## 1 35321.46
## 2 45115.53
## 3 42925.71
## 4 67422.36
## 5 55915.46
## 6 56612.00
In this homework, I used data set of Car Sales Price Prediction. Firstly I’ve imported the data into a variable \(mydata\). I’ve also renamed variables into more appropriate ones.
Description of variables:
Data source: Kaggle - https://www.kaggle.com/datasets/yashpaloswal/ann-car-sales-price-prediction
I’ve decided to use this data, because I think that I could find
some interesting findings about car sales price. I’m interested to see,
if there is any difference in the car purchase amount between males and
females.
mydata$genderF <- factor(mydata$gender, #created factor for Male and Female
levels = c(0, 1),
labels = c("F", "M"))
mydata$countryF <- factor(mydata$country) #created factor for countries
library(tidyr)
mydata <- drop_na(mydata) #remove NA values
mydata$age <- round(mydata$age, 0) #round the age to the full value
mydata$ID <- seq(1, nrow(mydata)) #create variable ID
library(psych)
summary(mydata)
## name e-mail country gender
## Length:500 Length:500 Length:500 Min. :0.000
## Class :character Class :character Class :character 1st Qu.:0.000
## Mode :character Mode :character Mode :character Median :1.000
## Mean :0.506
## 3rd Qu.:1.000
## Max. :1.000
##
## age annual_salary credit_card_depth net_worth
## Min. :20.00 Min. : 20000 Min. : 100 Min. : 20000
## 1st Qu.:41.00 1st Qu.: 54392 1st Qu.: 7398 1st Qu.: 299824
## Median :46.00 Median : 62916 Median : 9655 Median : 426750
## Mean :46.22 Mean : 62127 Mean : 9608 Mean : 431476
## 3rd Qu.:52.00 3rd Qu.: 70118 3rd Qu.:11799 3rd Qu.: 557325
## Max. :70.00 Max. :100000 Max. :20000 Max. :1000000
##
## car_purchase_amount genderF countryF ID
## Min. : 9000 F:247 Bolivia : 6 Min. : 1.0
## 1st Qu.:37630 M:253 Israel : 6 1st Qu.:125.8
## Median :43998 Mauritania : 6 Median :250.5
## Mean :44210 Saint Barth\xe9lemy: 5 Mean :250.5
## 3rd Qu.:51255 Algeria : 5 3rd Qu.:375.2
## Max. :80000 Armenia : 5 Max. :500.0
## (Other) :467
Here I’ve firstly created a factor of the variable gender, which means, that I’ve changed the values \(0\) and \(1\) into the values \(F\) and \(M\). I’ve also factored the values of the variable country. The next thing that I did, is that I’ve removed the NA values and rounded age to the full value (e.g. \(43.58\) to \(44\)). I’ve also created the variable ID.
We are using data about \(500\) different consumers which age range varies between \(20\) and \(70\). Average consumer is \(46.24\) years old. The median age is \(46\) years. Maximum price that consumer is willing to pay is \(80000€\) and the minimum is \(9000€\). In our sample we have \(247\) females and \(253\) males. The average annual salary is \(63127€\) and the average credit card depth of a consumer is \(9608€\). The 1st Qu. of the variable net_worth is \(299824€\), the second one(median) is \(426750€\) and the 3rd Qu. is \(557325€\).
Now I’m going to do descriptive statistics of the car_purchase_amount based on the Gender.
describeBy(mydata$car_purchase_amount, mydata$genderF)
##
## Descriptive statistics by group
## group: F
## vars n mean sd median trimmed mad min max range
## X1 1 247 44933.13 10325.99 44463.31 44948.67 9377.04 9000 70878.3 61878.3
## skew kurtosis se
## X1 -0.14 0.3 657.03
## ------------------------------------------------------------
## group: M
## vars n mean sd median trimmed mad min max
## X1 1 253 43503.62 11167.77 42909.27 43389.39 10505.28 10092.23 80000
## range skew kurtosis se
## X1 69907.77 0.08 0.13 702.11
Females are on average willing to spend more than males (\(females = 44933.13€\) and \(males = 43503.62€\)), but the minimum amount that consumer is willing to pay for a car is higher for males \(10092.23€\) than for females \(9000€\). The maximum amount is also higher for males \(80000€\). Based on the skewness, the distribution for females is slightly asymmetrical to the left (\(skew=-0.14\)).
Here we are going to check the distribution of age of consumers with
the help of a histogram.
#we are checking the distribution of age of consumers
hist(mydata$age,
main = "Distribution of age of consumers",
xlab = "Age",
ylab = "Frequency")
Based on the data, the research question would be: Is there a difference in a average amount of money that consumers would spend, between \(males\) and \(females\)? Based on this I’ve determined the \(H_{0}\) and \(H_{1}\).
\(H_{0}\): Difference
between average amount that consumers would spend to purchase car is
equal to 0 between males and females
\(H_{1}\): Difference between average
amount that consumers would spend to purchase car is not equal to 0
between males and females
or more specific:
\(H_{0}\): \(\mu_{males}\) = \(\mu_{females}\) => \(\mu_{males}\) - \(\mu_{females}\) = 0
\(H_{1}\): \(\mu_{males}\) \(\neq\) \(\mu_{females}\) => \(\mu_{males}\) - \(\mu_{females}\) \(\neq\) 0
describeBy(mydata$car_purchase_amount, mydata$genderF)
##
## Descriptive statistics by group
## group: F
## vars n mean sd median trimmed mad min max range
## X1 1 247 44933.13 10325.99 44463.31 44948.67 9377.04 9000 70878.3 61878.3
## skew kurtosis se
## X1 -0.14 0.3 657.03
## ------------------------------------------------------------
## group: M
## vars n mean sd median trimmed mad min max
## X1 1 253 43503.62 11167.77 42909.27 43389.39 10505.28 10092.23 80000
## range skew kurtosis se
## X1 69907.77 0.08 0.13 702.11
Firstly we need to check the assumptions. We check if the data is Normaly distributed.
#with the help of histograms we show the distribution by gender
library(ggplot2)
female <- ggplot(mydata[mydata$genderF == "F", ], aes(x = car_purchase_amount)) +
theme_linedraw() +
geom_histogram() +
ylab("Count") +
ggtitle("Females")
male <- ggplot(mydata[mydata$genderF == "M", ], aes(x = car_purchase_amount)) +
theme_linedraw() +
geom_histogram() +
ylab("Count") +
ggtitle("Males")
library(ggpubr)
ggarrange(female, male,
ncol = 2, nrow = 1)
# To check normality we use Shapiro-Wilk test
library(dplyr)
library(rstatix)
mydata %>%
group_by(genderF) %>%
shapiro_test(car_purchase_amount)
## # A tibble: 2 × 4
## genderF variable statistic p
## <fct> <chr> <dbl> <dbl>
## 1 F car_purchase_amount 0.994 0.388
## 2 M car_purchase_amount 0.996 0.814
Based on the Shapiro-Wilk test, the distributions are normal (\(p=0.388\) for females and \(p=0.814\) for males and both are greater than \(\alpha\)), which means that this assumption is met, so because of that we choose the parametric test, that is Independent samples t-test. We assume that the variances are not equal, so we use the Welch Two Sample t-test (applied Welch correction).
t.test(mydata$car_purchase_amount ~ mydata$genderF,
paired = FALSE,
var.equal = FALSE,
alternative = "two.sided")
##
## Welch Two Sample t-test
##
## data: mydata$car_purchase_amount by mydata$genderF
## t = 1.4866, df = 496.54, p-value = 0.1377
## alternative hypothesis: true difference in means between group F and group M is not equal to 0
## 95 percent confidence interval:
## -459.7655 3318.7880
## sample estimates:
## mean in group F mean in group M
## 44933.13 43503.62
Based on the data we do not reject \(H_{0}\) (\(p=0.14\)). Now we are going to interpret the effect size.
library(effectsize)
effectsize::cohens_d(mydata$car_purchase_amount ~ mydata$genderF,
pooled_sd=FALSE)
## Cohen's d | 95% CI
## -------------------------
## 0.13 | [-0.04, 0.31]
##
## - Estimated using un-pooled SD.
interpret_cohens_d(0.13, rules="sawilowsky2009")
## [1] "very small"
## (Rules: sawilowsky2009)
Conclusion: Using the sample data, we are demonstrated, that
the amount that consumers are willing to pay has a very small
difference between males and females (\(d=0.13\)).
If the assumption of normality wouldn’t be met, we would need
to use non-parametric test Wilcoxon Rank Sum Test, as we would
compare medians. The hypothesis would look like:
\(H_{0}\): Location distributions of car
purchase amount are the same for males and females
\(H_{1}\): Location distribution of car
purchase amount are different for males and females
wilcox.test(mydata$car_purchase_amount ~ mydata$genderF,
paired = FALSE,
correct = FALSE,
exact = FALSE,
alternative = "two.sided")
##
## Wilcoxon rank sum test
##
## data: mydata$car_purchase_amount by mydata$genderF
## W = 34036, p-value = 0.08406
## alternative hypothesis: true location shift is not equal to 0
Based on the data we would not reject \(H_{0}\) (\(p=0.08\)). The effect size would look like:
library(effectsize)
effectsize(wilcox.test(mydata$car_purchase_amount ~ mydata$genderF,
pooled_sd=FALSE))
## r (rank biserial) | 95% CI
## ---------------------------------
## 0.09 | [-0.01, 0.19]
interpret_rank_biserial(0.09)
## [1] "very small"
## (Rules: funder2019)
As I’ve already mentioned before, based on the assumptions, the more appropriate test would be the parametric one (Independent samples t-test). We would only use the non-parametric test, if one of the assumptions wouldn’t be met (normality). To sum this up, the conclusion would be: Using the sample data, we’ve demonstrated, that the amount of money that consumers are willing to pay has a very small difference between males and females, based on the effect size (\(d=0.13\)).
In this part of the homework, I’ve used the same dataset as
before in Hypothesis testing, as I think that is also suitable for
regression analysis. As can be seen in the name of this section the
research question is: To what extent do age, annual salary, credit
card debt, net worth, and gender influence the overall amount that
consumers would spend to purchase a car?
In this section of the homework I’ve used she same data set of Car Sales Price Prediction.
head(mydata)
## name e-mail
## 1 Martina Avila cubilia.Curae.Phasellus@quisaccumsanconvallis.edu
## 2 Harlan Barnes eu.dolor@diam.co.uk
## 3 Naomi Rodriquez vulputate.mauris.sagittis@ametconsectetueradipiscing.co.uk
## 4 Jade Cunningham malesuada@dignissim.com
## 5 Cedric Leach felis.ullamcorper.viverra@egetmollislectus.net
## 6 Carla Hester mi@Aliquamerat.edu
## country gender age annual_salary credit_card_depth net_worth
## 1 Bulgaria 0 42 62812.09 11609.381 238961.3
## 2 Belize 0 41 66646.89 9572.957 530973.9
## 3 Algeria 1 43 53798.55 11160.355 638467.2
## 4 Cook Islands 1 58 79370.04 14426.165 548599.1
## 5 Brazil 1 57 59729.15 5358.712 560304.1
## 6 Liberia 1 57 68499.85 14179.472 428485.4
## car_purchase_amount genderF countryF ID
## 1 35321.46 F Bulgaria 1
## 2 45115.53 F Belize 2
## 3 42925.71 M Algeria 3
## 4 67422.36 M Cook Islands 4
## 5 55915.46 M Brazil 5
## 6 56612.00 M Liberia 6
Description of variables:
Data source: Kaggle - https://www.kaggle.com/datasets/yashpaloswal/ann-car-sales-price-prediction
As mentioned above, the research question of this part of the homework for the regression analysis is: To what extent do age, annual salary, credit card debt, net worth, and gender influence the overall amount that consumers would spend to purchase a car?. To answer this question I’m going to predict the car purchase amount (variable car_purchase_amount), using multiple explanatory variables. Firstly I’m going to present the variables:
Dependend Variable:
Explanatory Variables:
Gender is used as a Dummy variable, so that’s why we use is as a factor. This was already done in the first part of the homework.
What effects are expected from each explanatory variable on the dependent variable:
Regression function:
\({Y} = b_{0} + b_{1}X_{1} + b_{2}X_{2} + b_{3}X_{3} + b_{4}X_{4} + b_{5}X_{5} + \epsilon\)
\(X_{5}\) is a dummy variable, so the regression function can be written like this:
\({Y} = b_{0} + b_{1}X_{1} + b_{2}X_{2} + b_{3}X_{3} + b_{4}X_{4} + b_{5}D + \epsilon\)
or
\({car\_purchase\_amount} = b_{0} + b_{1}age + b_{2}anual\_salary + b_{3}credit\_card\_depth + b_{4}net\_worth + b_{5}genderF + \epsilon\)
Estimated egression function:
\(\hat{Y} = b_{0} + b_{1}X_{1} + b_{2}X_{2} + b_{3}X_{3} + b_{4}X_{4} + b_{5}D\)
or
\(\hat{car\_purchase\_amount} = b_{0} + b_{1}age + b_{2}anual\_salary + b_{3}credit\_card\_depth + b_{4}net\_worth + b_{5}genderF\)
Now I’m going to show you some descriptive statistics. As I’ve already mentioned before, I’ve used the same data as for Hypothesis testing. In this case, I’ve used different function as before, but it was also used in the lectures.
#summary(mydata)
library(pastecs)
round(stat.desc(mydata[, c(-1, -2, -3, -4, -10, -11, -12)]), 2)
## age annual_salary credit_card_depth net_worth
## nbr.val 500.00 500.00 500.00 5.000000e+02
## nbr.null 0.00 0.00 0.00 0.000000e+00
## nbr.na 0.00 0.00 0.00 0.000000e+00
## min 20.00 20000.00 100.00 2.000000e+04
## max 70.00 100000.00 20000.00 1.000000e+06
## range 50.00 80000.00 19900.00 9.800000e+05
## sum 23112.00 31063619.80 4803822.52 2.157379e+08
## median 46.00 62915.50 9655.04 4.267501e+05
## mean 46.22 62127.24 9607.65 4.314757e+05
## SE.mean 0.36 523.39 156.04 7.760800e+03
## CI.mean.0.95 0.70 1028.32 306.58 1.524787e+04
## var 63.85 136969061.94 12174432.71 3.011501e+10
## std.dev 7.99 11703.38 3489.19 1.735368e+05
## coef.var 0.17 0.19 0.36 4.000000e-01
## car_purchase_amount
## nbr.val 500.00
## nbr.null 0.00
## nbr.na 0.00
## min 9000.00
## max 80000.00
## range 71000.00
## sum 22104899.61
## median 43997.78
## mean 44209.80
## SE.mean 481.79
## CI.mean.0.95 946.59
## var 116061380.26
## std.dev 10773.18
## coef.var 0.24
We are using data about \(500\) different consumers which age range varies between \(20\) and \(70\). Average consumer is \(46.22\) years old. The median age is \(46\) years. Maximum price that consumer is willing to pay is \(80000€\) and the minimum is \(9000€\). The average annual salary is \(62127.24€\) and the average credit card depth of a consumer is \(9607.65€\). The median of the variable net_worth is \(4.267501e+05\) which is equal to \(426750.1€\). The minimum amount of credit card depth is \(100€\) and maximum amount is \(20000€\), which means that everyone is in dept. Now I’m going to do some descriptive statistics of car purchase amount based on the gender.
describeBy(mydata$car_purchase_amount, mydata$genderF)
##
## Descriptive statistics by group
## group: F
## vars n mean sd median trimmed mad min max range
## X1 1 247 44933.13 10325.99 44463.31 44948.67 9377.04 9000 70878.3 61878.3
## skew kurtosis se
## X1 -0.14 0.3 657.03
## ------------------------------------------------------------
## group: M
## vars n mean sd median trimmed mad min max
## X1 1 253 43503.62 11167.77 42909.27 43389.39 10505.28 10092.23 80000
## range skew kurtosis se
## X1 69907.77 0.08 0.13 702.11
In our sample we have \(247\) females and \(253\) males. Females are on average willing to spend more than males (\(females = 44933.13€\) and \(males = 43503.62€\)), but the minimum amount that consumer is willing to pay for a car is higher for males \(10092.23€\) than for females \(9000€\). The maximum amount is also higher for males \(80000€\). Based on the skewness, the distribution for females is slightly asymmetrical to the left (\(skew=-0.14\)).
Based on the data, the research question would be: To what extent do age, annual salary, credit card debt, net worth, and gender influence the overall amount that consumers would spend to purchase a car? As seen before we have dependent variable and multiple explanatory variables. We also have one dummy variable gender. We are going to use Multiple linear regression, but first, we need to check the Assumptions.
Firstly we need to check the assumptions.
library(car)
scatterplotMatrix(mydata[,c("car_purchase_amount", "age", "annual_salary", "credit_card_depth", "net_worth")], smooth=FALSE)
library(Hmisc)
rcorr(as.matrix(mydata[, c("car_purchase_amount", "age", "annual_salary", "credit_card_depth", "net_worth")]))
## car_purchase_amount age annual_salary credit_card_depth
## car_purchase_amount 1.00 0.63 0.62 0.03
## age 0.63 1.00 0.00 0.03
## annual_salary 0.62 0.00 1.00 0.05
## credit_card_depth 0.03 0.03 0.05 1.00
## net_worth 0.49 0.02 0.01 -0.05
## net_worth
## car_purchase_amount 0.49
## age 0.02
## annual_salary 0.01
## credit_card_depth -0.05
## net_worth 1.00
##
## n= 500
##
##
## P
## car_purchase_amount age annual_salary credit_card_depth
## car_purchase_amount 0.0000 0.0000 0.5193
## age 0.0000 0.9936 0.4788
## annual_salary 0.0000 0.9936 0.2683
## credit_card_depth 0.5193 0.4788 0.2683
## net_worth 0.0000 0.6268 0.7419 0.2704
## net_worth
## car_purchase_amount 0.0000
## age 0.6268
## annual_salary 0.7419
## credit_card_depth 0.2704
## net_worth
fit <- lm(car_purchase_amount ~ age + annual_salary + credit_card_depth + net_worth + genderF,
data = mydata)
vif(fit)
## age annual_salary credit_card_depth net_worth
## 1.006113 1.004205 1.006873 1.003318
## genderF
## 1.006612
\(genderF\) is a variable that affects the dependent variable, so I needed to include it in the model.
mean(vif(fit))
## [1] 1.005424
VIF values are very close to 1, suggesting that there is little to no evidence of multicollinearity among independent variables. This means that there is no perfect multicollinearity and we can proceed with the regression model, as this assumption is met.
mydata$StdResid <- round(rstandard(fit), 3) #Standarized residuals
mydata$CooksD <- round(cooks.distance(fit), 3) #Cooks distances
hist(mydata$StdResid,
xlab = "Standardized residuals",
ylab = "Frequency",
main = "Histrogram of standarized residuals")
We check if standardized residuals are normally distributed with the help of the Shapiro-Wilk normality test.
shapiro.test(mydata$StdResid)
##
## Shapiro-Wilk normality test
##
## data: mydata$StdResid
## W = 0.95936, p-value = 1.624e-10
Based on the results, the Standard residuals are not normally distributed, but since we have a large sample size \(n>30\), this is less critical.
Next we need to check the Cooks distances with the help of Histogram.
hist(mydata$CooksD,
xlab = "Cooks distance",
ylab = "Frequency",
main = "Histogram of Cooks distances")
As we can see, there are some gaps in the histogram of Cook’s distances. This is going to be fixed, but before proceeding, it is crucial to examine whether there are any problematic standard residuals (outliers).
head(mydata[order(mydata$StdResid),], 3) #Three units with the lowest value of stand. residuals
## name e-mail country gender
## 139 Yoshio Potter fringilla.cursus.purus@dolorvitae.ca Martinique 1
## 462 Hillary ut.mi.Duis@quisarcu.com South Sudan 1
## 357 Lawrence, Hope Z. metus.vitae@Morbisitamet.ca Switzerland 1
## age annual_salary credit_card_depth net_worth car_purchase_amount genderF
## 139 45 52632.97 12348.68 293999.9 33428.40 M
## 462 39 58632.59 12035.37 516817.3 38243.06 M
## 357 46 54362.70 15647.35 388898.1 38042.80 M
## countryF ID StdResid CooksD
## 139 Martinique 139 -1.935 0.005
## 462 South Sudan 462 -1.787 0.004
## 357 Switzerland 357 -1.780 0.006
head(mydata[order(-mydata$StdResid),], 3) #Three units with the highest value of stand. residuals
## name e-mail
## 291 Abigail X. Lindsey dui@nondui.ca
## 453 Harriet amet.orci@tellus.net
## 105 Yasir Tyler pharetra.sed.hendrerit@adipiscinglobortisrisus.edu
## country gender age annual_salary credit_card_depth net_worth
## 291 Croatia 0 63 46549.16 640.0454 626163.8
## 453 Mali 0 47 58260.57 4658.4145 507572.6
## 105 Qatar 0 48 63975.06 10614.8545 891439.9
## car_purchase_amount genderF countryF ID StdResid CooksD
## 291 55543.38 F Croatia 291 1.811 0.017
## 453 45271.46 F Mali 453 1.754 0.004
## 105 60461.24 F Qatar 105 1.751 0.010
Here we can see, that there are no problematic Standard residuals (no outliers). As we mentioned before, we saw that there are some gaps in Cook’s distances with the help of the Histogram, but now we need to confirm this and remove the problematic ones.
head(mydata[order(-mydata$CooksD),], 8) #Eight units with highest value of Cooks distance
## name e-mail
## 291 Abigail X. Lindsey dui@nondui.ca
## 55 Rafael Peterson lectus.a.sollicitudin@tortorat.net
## 237 Tate C. Foster dignissim.magna@Maurisvel.edu
## 467 India risus.at.fringilla@feugiat.ca
## 105 Yasir Tyler pharetra.sed.hendrerit@adipiscinglobortisrisus.edu
## 441 Ora bibendum.Donec.felis@liberoestcongue.org
## 232 Emerald U. Hanson dui.quis.accumsan@pedeCrasvulputate.co.uk
## 301 Oconnor, Linus Q. Donec.elementum@Fuscemilorem.co.uk
## country gender age annual_salary credit_card_depth net_worth
## 291 Croatia 0 63 46549.16 640.0454 626163.83
## 55 Sri Lanka 1 52 35069.42 1851.9798 353757.51
## 237 Viet Nam 1 70 41434.51 6810.5556 252220.29
## 467 Nigeria 0 55 32697.98 10858.0253 218808.76
## 105 Qatar 0 48 63975.06 10614.8545 891439.88
## 441 Uruguay 1 40 57777.16 19692.9126 601210.28
## 232 United Arab Emirates 0 49 77435.47 6922.1528 48620.32
## 301 Mayotte 0 51 82094.11 1065.6076 577272.68
## car_purchase_amount genderF countryF ID StdResid CooksD
## 291 55543.38 F Croatia 291 1.811 0.017
## 55 31978.98 M Sri Lanka 55 1.713 0.013
## 237 47009.58 M Viet Nam 237 -1.456 0.012
## 467 28463.64 F Nigeria 467 -1.730 0.012
## 105 60461.24 F Qatar 105 1.751 0.010
## 441 41147.47 M Uruguay 441 -1.547 0.010
## 232 44432.72 F United Arab Emirates 232 1.634 0.009
## 301 63248.76 F Mayotte 301 -1.488 0.009
As we can see in the data, we need to remove the consumers with the ID \(291\) and \(467\), because the gap in Cook’s distances is too big.
library(dplyr)
mydata <- mydata %>% filter(!ID == 291 & !ID == 467)
Now we are going to repeat the process to verify if there has been any improvement.
fit <- lm(car_purchase_amount ~ age + annual_salary + credit_card_depth + net_worth + genderF,
data = mydata)
mydata$StdResid <- round(rstandard(fit), 3) #Standarized residuals
mydata$CooksD <- round(cooks.distance(fit), 3) #Cooks distances
hist(mydata$StdResid,
xlab = "Standardized residuals",
ylab = "Frequency",
main = "Histrogram of standarized residuals")
shapiro.test(mydata$StdResid)
##
## Shapiro-Wilk normality test
##
## data: mydata$StdResid
## W = 0.96023, p-value = 2.412e-10
As we mentioned before, based on the results, the Standard residuals are not normally distributed, but since we have a large sample size \(n>30\), this is less critical.
hist(mydata$CooksD,
xlab = "Cooks distance",
ylab = "Frequency",
main = "Histogram of Cooks distances")
If we now check the Histogram, we can see that there are no more gaps in the Cook’s distances. Now we need to check the assumption of Homoskedasticity.
mydata$StdFitted <- scale(fit$fitted.values)
library(car)
scatterplot(y = mydata$StdResid, x = mydata$StdFitted,
ylab = "Standardized residuals",
xlab = "Standardized fitted values",
boxplots = FALSE,
regLine = FALSE,
smooth = FALSE)
We can already roughly see from the scatter plot that the assumption of homoscedasticity is met, but we further verify this with the Breusch Pagan test for Heteroskedasticity.
library(olsrr)
ols_test_breusch_pagan(fit)
##
## Breusch Pagan Test for Heteroskedasticity
## -----------------------------------------
## Ho: the variance is constant
## Ha: the variance is not constant
##
## Data
## -----------------------------------------------
## Response : car_purchase_amount
## Variables: fitted values of car_purchase_amount
##
## Test Summary
## -----------------------------
## DF = 1
## Chi2 = 0.07506219
## Prob > Chi2 = 0.784104
As we can see from the Breusch Pagan test, the assumption of Homoscedasticity is met \(p=0.78\) which means that \(p>0.05\)
summary(fit)
##
## Call:
## lm(formula = car_purchase_amount ~ age + annual_salary + credit_card_depth +
## net_worth + genderF, data = mydata)
##
## Residuals:
## Min 1Q Median 3Q Max
## -470.20 -206.61 19.87 202.71 426.18
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -4.208e+04 9.416e+01 -446.896 <2e-16 ***
## age 8.397e+02 1.360e+00 617.390 <2e-16 ***
## annual_salary 5.621e-01 9.302e-04 604.270 <2e-16 ***
## credit_card_depth 6.297e-03 3.119e-03 2.019 0.044 *
## net_worth 2.892e-02 6.234e-05 464.005 <2e-16 ***
## genderFM 2.488e+01 2.164e+01 1.150 0.251
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 240.7 on 492 degrees of freedom
## Multiple R-squared: 0.9995, Adjusted R-squared: 0.9995
## F-statistic: 1.986e+05 on 5 and 492 DF, p-value: < 2.2e-16
sqrt(summary(fit)$r.squared) #Coefficient of multiple correlation
## [1] 0.9997523
Based on F-statistics, we reject \(H_0\) based on \(p<0.001\) (\(H_{0}: \rho^{2}=0\) and \(H_{1}: \rho^{2}>0\)). The \(\rho^{2}>0\), which means, that we found
linear relationship between \(car\_purchase\_amount\) and explanatory
variables.
Linear relationship between \(car\_purchase\_amount\) and other
explanatory variables is positive and strength is very
strong (0.999).
Coefficient of determination: 99.95% of
variability is explained by linear effect.
We can only
explain partial regression coefficients that are statistically
significant, which means that we can’t explain \(genderF\), because it’s not statistically
significant (\(p>0.5\)).
Age: If age increases by 1 year on average, the car purchase
amount (amount that consumers are willing to pay for a car) increases by
\(839.7€\) (\(p<0.001\)) on average, assuming all
other explanatory variables stays the same.
Annual_salary:
If annual salary increases by 1€ on average, the car purchase amount
increases by \(0.56€\) (\(p<0.001\)) on average, assuming all
other explanatory variables stays the same.
Credit_card_depth: If credit card depth increases by 1€ on
average, the car purchase amount increases by \(0.006€\) (\(p<0.001\)) on average, assuming all
other explanatory variables stays the same.
Net_worth: If
net worth increases by 1€ on average, the car purchase amount increases
by \(0.03€\) (\(p<0.001\)) on average, assuming all
other explanatory variables stays the same.
mydata$Fitted <- fitted.values(fit)
mydata$Residuals <- residuals(fit)
head(mydata[colnames(mydata) %in% c("name", "car_purchase_amount", "Fitted", "Residuals")])
## name car_purchase_amount Fitted Residuals
## 1 Martina Avila 35321.46 35479.40 -157.93730
## 2 Harlan Barnes 45115.53 45228.43 -112.90914
## 3 Naomi Rodriquez 42925.71 42830.12 95.58776
## 4 Jade Cunningham 67422.36 67220.62 201.74137
## 5 Cedric Leach 55915.46 55622.61 292.85739
## 6 Carla Hester 56612.00 56795.27 -183.27331
This output provides a snapshot of how well regression model predicted car purchase amount for each individual, where residuals indicates difference between actual and predicted values. Positive residuals suggest that the model overestimates car purchase amounts for individuals and negative residuals suggest that the model underestimates car purchase amounts for individuals.
Interpretation: