source: Audi A3 user car prices. (n.d.). Www.kaggle.com. Retrieved January 5, 2024, from https://www.kaggle.com/datasets/kevbradwick/audi-a3-used-prices
Research question: How do factors such as year, mileage, style, transmission type, engine volume, and fuel type influence the resale price of an Audi A3?
library(readr)
mydata2 <- read_csv("audi_a3.csv")
## Rows: 280 Columns: 12
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (7): title, subtitle, style, transmission, fuel, make, model
## dbl (5): ID, price, year, mileage, engine
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
head(mydata2, 10)
## # A tibble: 10 × 12
## ID price title subtitle year style mileage engine transmission fuel
## <dbl> <dbl> <chr> <chr> <dbl> <chr> <dbl> <dbl> <chr> <chr>
## 1 1 17524 Audi A3 1.4 TFSI e… 2017 Hatc… 48256 1.4 Automatic Petr…
## 2 2 3000 Audi A3 2.0 TDI S … 2006 Hatc… 138000 2 Manual Dies…
## 3 3 2390 Audi A3 2.0 TDI Sp… 2011 Hatc… 190000 2 Manual Dies…
## 4 4 1275 Audi A3 1.6 Specia… 2006 Hatc… 131000 1.6 Manual Petr…
## 5 5 1200 Audi A3 2.0 FSI Sp… 2005 Hatc… 121490 2 Manual Petr…
## 6 6 3995 Audi A3 1.6L SPORT… 2010 Hatc… 85000 1.6 Manual Petr…
## 7 7 3309 Audi A3 2.0 TDI S … 2007 Hatc… 97827 2 Manual Dies…
## 8 8 1989 Audi A3 2.0 TDI SE… 2007 Hatc… 141000 2 Manual Dies…
## 9 9 4595 Audi A3 1.6 Techni… 2011 Hatc… 88721 1.6 Manual Petr…
## 10 10 1395 Audi A3 2.0 TDI Sp… 2005 Hatc… 171500 2 Manual Dies…
## # ℹ 2 more variables: make <chr>, model <chr>
# Removing coloumns that will not be included in the analysis
mydata2 <- mydata2[,-c(3, 4, 11, 12)]
head(mydata2, 10)
## # A tibble: 10 × 8
## ID price year style mileage engine transmission fuel
## <dbl> <dbl> <dbl> <chr> <dbl> <dbl> <chr> <chr>
## 1 1 17524 2017 Hatchback 48256 1.4 Automatic Petrol
## 2 2 3000 2006 Hatchback 138000 2 Manual Diesel
## 3 3 2390 2011 Hatchback 190000 2 Manual Diesel
## 4 4 1275 2006 Hatchback 131000 1.6 Manual Petrol
## 5 5 1200 2005 Hatchback 121490 2 Manual Petrol
## 6 6 3995 2010 Hatchback 85000 1.6 Manual Petrol
## 7 7 3309 2007 Hatchback 97827 2 Manual Diesel
## 8 8 1989 2007 Hatchback 141000 2 Manual Diesel
## 9 9 4595 2011 Hatchback 88721 1.6 Manual Petrol
## 10 10 1395 2005 Hatchback 171500 2 Manual Diesel
Description: ID: id of a car price: The price in GBP year: The year of manufacture style: The car style (Hatchback, Saloon) mileage: The mileage of the car in miles engine: The engine size in litres transmission: The vehicle transmission (Automatic/Manual) fuel: The fuel type (Diesel, Petrol, Electric)
# Function to check for any missing value in a row
row_has_na <- function(row) {
return(any(is.na(row)))
}
rows_with_na <- apply(mydata2, 1, row_has_na)
rows_with_missing_values <- which(rows_with_na == TRUE)
print(rows_with_missing_values)
## [1] 14 24 28 42 56 70 82 84 98 112 126 140 154 168 182 196 210 224 238
## [20] 252 266 280
rows_to_remove <- c(14, 24, 28, 42, 56, 70, 82, 84, 98, 112, 126, 140, 154, 168, 182, 196, 210, 224, 238, 252, 266, 280)
mydata2 <- mydata2[-rows_to_remove, ]
print(mydata2)
## # A tibble: 258 × 8
## ID price year style mileage engine transmission fuel
## <dbl> <dbl> <dbl> <chr> <dbl> <dbl> <chr> <chr>
## 1 1 17524 2017 Hatchback 48256 1.4 Automatic Petrol
## 2 2 3000 2006 Hatchback 138000 2 Manual Diesel
## 3 3 2390 2011 Hatchback 190000 2 Manual Diesel
## 4 4 1275 2006 Hatchback 131000 1.6 Manual Petrol
## 5 5 1200 2005 Hatchback 121490 2 Manual Petrol
## 6 6 3995 2010 Hatchback 85000 1.6 Manual Petrol
## 7 7 3309 2007 Hatchback 97827 2 Manual Diesel
## 8 8 1989 2007 Hatchback 141000 2 Manual Diesel
## 9 9 4595 2011 Hatchback 88721 1.6 Manual Petrol
## 10 10 1395 2005 Hatchback 171500 2 Manual Diesel
## # ℹ 248 more rows
#here I removed rows with missing values
library(pastecs)
round(stat.desc(mydata2[ , c(-1, -4, -7, -8)]))
## price year mileage engine
## nbr.val 258 258 258 258
## nbr.null 0 0 0 0
## nbr.na 0 0 0 0
## min 1100 1997 16573 1
## max 24000 2021 288280 3
## range 22900 24 271707 2
## sum 1973356 518999 24176550 435
## median 5497 2011 91950 2
## mean 7649 2012 93708 2
## SE.mean 371 0 2623 0
## CI.mean.0.95 732 1 5166 0
## var 35600402 18 1775479457 0
## std.dev 5967 4 42136 0
## coef.var 1 0 0 0
library(car)
## Loading required package: carData
scatterplotMatrix(mydata2[ , c(-1, -4, -7, -8)], smooth = FALSE)
library(Hmisc)
##
## Attaching package: 'Hmisc'
## The following objects are masked from 'package:base':
##
## format.pval, units
rcorr(as.matrix(mydata2[ , c(-1, -4, -7, -8)]))
## price year mileage engine
## price 1.00 0.88 -0.80 -0.41
## year 0.88 1.00 -0.68 -0.45
## mileage -0.80 -0.68 1.00 0.44
## engine -0.41 -0.45 0.44 1.00
##
## n= 258
##
##
## P
## price year mileage engine
## price 0 0 0
## year 0 0 0
## mileage 0 0 0
## engine 0 0 0
#here I used the table of correlations to more easily interpret the data from the scatterplot matrix
#from the correlation matrix we can see that price and years are
positively correlated, meaning that the higher is the price as the
higher is the year of manufacture, meaning new cars are more expensive
which we expected to be.
#also we can see that mileage is negatively correlated with years
meaning that new cars have less miles what we expected, also we can see
negative correlation between price and mileage meaning that cars with
more miles are less expensive as we expected to be like that.
#from the matrix we can see also see that there is negative correlation
between engine and price which is expected as well
fit1 <- lm(price ~ year + style + mileage + engine + transmission + fuel, data = mydata2)
#first model of linear regression
library(car)
vif(fit1)
## year style mileage engine transmission fuel
## 2.367713 1.347159 2.222837 1.890318 1.173469 1.700135
#multicolinearity
mean(vif(fit1))
## [1] 1.783605
#number is close to 1 we should not remove variables from the fit1
mydata2$StdResid <- round(rstandard(fit1), 3)
mydata2$CooksD <- round(cooks.distance(fit1), 3)
hist(mydata2$StdResid,
xlab = "Standardized Residuals",
ylab = "Frequency",
main = "Histogram of standardized residuals"
)
#histogram for standardized residuals to see if some outliers should be removed
#histogram is asymetric to the right a little bit
shapiro.test(mydata2$StdResid)
##
## Shapiro-Wilk normality test
##
## data: mydata2$StdResid
## W = 0.93098, p-value = 1.314e-09
h0: errors are normally distributed h1: errors are not normally distributed reject H0 at p value < 0.001.
but as we have a big sample it is not really important
hist(mydata2$CooksD,
xlab = "Cooks distance",
ylab = "Frequency",
main = "Histogram of Cooks distances")
#here in the histogram we see many gaps and for this reason we should remove some rows
head(mydata2[order(mydata2$StdResid),], 3)
## # A tibble: 3 × 10
## ID price year style mileage engine transmission fuel StdResid CooksD
## <dbl> <dbl> <dbl> <chr> <dbl> <dbl> <chr> <chr> <dbl> <dbl>
## 1 103 8794 2015 Saloon 109000 1.4 Manual Petrol -3.00 0.112
## 2 116 13091 2015 Saloon 85150 1.4 Automatic Petrol -2.77 0.128
## 3 256 8240 2016 Hatchback 56500 1.4 Manual Petrol -2.13 0.012
#standardized residuals table with outliers to be removed
head(mydata2[order(-mydata2$CooksD),], 6)
## # A tibble: 6 × 10
## ID price year style mileage engine transmission fuel StdResid CooksD
## <dbl> <dbl> <dbl> <chr> <dbl> <dbl> <chr> <chr> <dbl> <dbl>
## 1 121 1295 1997 Hatchback 172000 1.8 Manual Petrol 5.06 0.251
## 2 135 1100 2008 Hatchback 288280 1.9 Manual Diesel 2.82 0.179
## 3 116 13091 2015 Saloon 85150 1.4 Automatic Petrol -2.77 0.128
## 4 240 2800 1999 Hatchback 104000 1.8 Manual Petrol 3.53 0.119
## 5 103 8794 2015 Saloon 109000 1.4 Manual Petrol -3.00 0.112
## 6 202 24000 2021 Hatchback 35563 1.5 Manual Petrol 4.01 0.111
#rows that show big differences in data collected that should be removed
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:Hmisc':
##
## src, summarize
## The following object is masked from 'package:car':
##
## recode
## The following objects are masked from 'package:pastecs':
##
## first, last
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
mydata2 <- mydata2 %>%
filter(!ID %in% c("116", "103", "256", "121", "135", "240", "202"))
#here I removed those rows
fit1 <- lm(price ~ year + style + mileage + engine + transmission + fuel, data = mydata2)
#new model with removed data
mydata2$StdResid <- round(rstandard(fit1), 3)
mydata2$CooksD <- round(cooks.distance(fit1), 3)
hist(mydata2$StdResid,
xlab = "Standardized Residuals",
ylab = "Frequency",
main = "Histogram of standardized residuals"
)
#checking again the distribution of errors
#again the graph is asymetrical to the right
shapiro.test(mydata2$StdResid)
##
## Shapiro-Wilk normality test
##
## data: mydata2$StdResid
## W = 0.95921, p-value = 1.542e-06
h0: errors are normally distributed h1: errors are not normally distributed reject H0 at p value < 0.001.
hist(mydata2$CooksD,
xlab = "Cooks distance",
ylab = "Frequency",
main = "Histogram of Cooks distances")
#here again we can see some gaps visible so we should remove more rows
head(mydata2[order(mydata2$StdResid),], 3)
## # A tibble: 3 × 10
## ID price year style mileage engine transmission fuel StdResid CooksD
## <dbl> <dbl> <dbl> <chr> <dbl> <dbl> <chr> <chr> <dbl> <dbl>
## 1 273 4995 2009 Hatchback 94000 1.8 Automatic Petrol -2.38 0.029
## 2 134 7495 2015 Hatchback 50000 1.2 Manual Petrol -2.34 0.019
## 3 204 3350 2010 Hatchback 71150 1.6 Manual Petrol -2.20 0.01
head(mydata2[order(-mydata2$CooksD),], 6)
## # A tibble: 6 × 10
## ID price year style mileage engine transmission fuel StdResid CooksD
## <dbl> <dbl> <dbl> <chr> <dbl> <dbl> <chr> <chr> <dbl> <dbl>
## 1 248 1500 2000 Hatchback 150000 1.8 Manual Petrol 4.25 0.131
## 2 45 1500 2004 Hatchback 197000 1.9 Manual Diesel 3.76 0.083
## 3 220 2495 2003 Hatchback 172700 2 Manual Diesel 4.32 0.083
## 4 80 10490 2013 Saloon 76220 2 Manual Diesel -2.19 0.063
## 5 12 16899 2018 Hatchback 55509 1 Manual Petrol 2.77 0.046
## 6 48 12698 2015 Saloon 67035 1.6 Manual Diesel -1.55 0.032
library(dplyr)
mydata2 <- mydata2 %>%
filter(!ID %in% c("273", "134", "204", "248", "45", "220", "80", "12", "48"))
#removing rows
fit1 <- lm(price ~ year + style + mileage + engine + transmission + fuel, data = mydata2)
#new model to see if now errors are normally distributed and that we dont have rows with significantly different values
mydata2$StdResid <- round(rstandard(fit1), 3)
mydata2$CooksD <- round(cooks.distance(fit1), 3)
hist(mydata2$StdResid,
xlab = "Standardized Residuals",
ylab = "Frequency",
main = "Histogram of standardized residuals"
)
shapiro.test(mydata2$StdResid)
##
## Shapiro-Wilk normality test
##
## data: mydata2$StdResid
## W = 0.99281, p-value = 0.2908
h0: errors are normally distributed h1: errors are not normally distributed now we cant reject H0 p value = 0.2908 meaning that errors are normally distributed
hist(mydata2$CooksD,
xlab = "Cooks distance",
ylab = "Frequency",
main = "Histogram of Cooks distances")
#here we still see some gaps where the data should be removed.
head(mydata2[order(-mydata2$CooksD),], 6)
## # A tibble: 6 × 10
## ID price year style mileage engine transmission fuel StdResid CooksD
## <dbl> <dbl> <dbl> <chr> <dbl> <dbl> <chr> <chr> <dbl> <dbl>
## 1 49 1675 2002 Hatchback 77074 1.6 Manual Petrol 2.22 0.05
## 2 18 3995 2005 Hatchback 153000 2 Automatic Diesel 2.10 0.044
## 3 86 1395 2002 Hatchback 79000 1.6 Manual Petrol 2.04 0.041
## 4 118 6200 2014 Hatchback 194005 1.6 Manual Diesel 1.80 0.039
## 5 10 1395 2005 Hatchback 171500 2 Manual Diesel 3.09 0.038
## 6 243 1990 2008 Hatchback 220000 3.2 Manual Petrol -0.713 0.037
library(dplyr)
mydata2 <- mydata2 %>%
filter(!ID %in% c("49", "18", "86", "118", "10", "243"))
#removing the data
fit1 <- lm(price ~ year + style + mileage + engine + transmission + fuel, data = mydata2)
#new model
hist(mydata2$CooksD,
xlab = "Cooks distance",
ylab = "Frequency",
main = "Histogram of Cooks distances")
#now we dont see any gaps so we dont have data that has large impact on
our analysis
mydata2$StdFitted <- scale(fit1$fitted.values)
library(car)
scatterplot(y = mydata2$StdResid, x = mydata2$StdFitted,
ylab = "Standardized Residuals",
xlab = "Standardized fitted values",
boxplots = FALSE,
regLine = FALSE,
smooth = FALSE)
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 = 2.152504
## Prob > Chi2 = 0.1423376
#we can’t reject H0 which means that the variance is constant
#we don’t have to use robust standard errors
summary(fit1)
##
## Call:
## lm(formula = price ~ year + style + mileage + engine + transmission +
## fuel, data = mydata2)
##
## Residuals:
## Min 1Q Median 3Q Max
## -3041.2 -737.1 100.9 962.2 3359.9
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -1.906e+06 6.717e+04 -28.369 < 2e-16 ***
## year 9.512e+02 3.309e+01 28.748 < 2e-16 ***
## styleSaloon 4.936e+03 3.979e+02 12.405 < 2e-16 ***
## mileage -3.714e-02 3.177e-03 -11.690 < 2e-16 ***
## engine 2.652e+03 4.570e+02 5.803 2.16e-08 ***
## transmissionManual -2.986e+03 2.193e+02 -13.619 < 2e-16 ***
## fuelPetrol 1.594e+03 2.153e+02 7.401 2.55e-12 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1152 on 229 degrees of freedom
## Multiple R-squared: 0.9635, Adjusted R-squared: 0.9626
## F-statistic: 1008 on 6 and 229 DF, p-value: < 2.2e-16
H0: ro2 = 0
H1: ro2 > 0
#model is good and we can reject H0 at p value < 0.001. (p value =
2.2e-16)
#based on standardized coefficinets we can compare all 6 variables
sqrt(summary(fit1)$r.squared)
## [1] 0.9815862
#linear relationship between price of Audi and all 6 explanatory variables is very strong
library(lm.beta)
lm.beta(fit1)
##
## Call:
## lm(formula = price ~ year + style + mileage + engine + transmission +
## fuel, data = mydata2)
##
## Standardized Coefficients::
## (Intercept) year styleSaloon mileage
## NA 0.6170351 0.1962341 -0.2388071
## engine transmissionManual fuelPetrol
## 0.1087680 -0.1939993 0.1341010
#smallest impact was found with engine, it is followed by
transmission type and style
#highest impact has year and followed by mileage
#conclusion
#In linear regression analysis of the Audi A3’s price, we considered six
explanatory variables: year, mileage, style, transmission type, engine
volume, and fuel type. The results revealed that the year of the vehicle
had the most significant impact on the price, closely followed by the
mileage. Lesser but still notable effects were observed with the style
and transmission type, while the engine volume exhibited the smallest
influence. Overall, the linear relationship between the price of the
Audi A3 and these six variables was found to be exceptionally strong,
with a correlation coefficient of 0.98, indicating a very high level of
predictability in the model.”