Assignment 3

Summary:

This data set involves cars and various information about them. The data set car_data includes the variables: name, year, selling price, present price, kilometers driven, fuel type, seller type, transmission, and owner. The total sample size is 301. This analysis seeks to run a multi-linear regression, analyze conditions, transform the response variable with a box-cox, perform a goodness of fit test, build candidate models, summarize the output, and interpret the regression coefficient.In this particular instance the response variable will be selling price and the two predictor variables are year and kilometers. After building several candidate models, the full model has the highest R-squared value at 1, but this likely means it suffers from over fitting and may suffer from problems. Instead, the best candidate is the square root model, as it has a higher r squared value then the log transformation at .1084 and also lower SSE, AIC, and SBC values. This means that 10.84% of the variability in the response variable price is explained by the predictor variables.The coefficients of the model are significant at a p-value of 0.05, so the model can be used to make predictions. However, with the R-square only being 10% the effect size is considered low or weak.

car_data <- read.csv("https://raw.githubusercontent.com/Kyle-Weber/STA321/main/www/car%20data.csv", header = TRUE)


car <- car_data[, -1]
price <- car$Selling_Price
year <- car$Year 
plot(year, price, main = "Year vs Price in cars")
abline(v=121.529, h=24.96, col="red", lty=2)

Here is the full linear model.

library(kableExtra)
full.model = lm(price ~ ., data = car)
kable(summary(full.model)$coef, caption ="Statistics of Regression Coefficients")
## Warning in summary.lm(full.model): essentially perfect fit: summary may be
## unreliable
Statistics of Regression Coefficients
Estimate Std. Error t value Pr(\>\|t\|)
(Intercept) 0 0 -3.363786e+00 0.0008719
Year 0 0 3.384092e+00 0.0008122
Selling_Price 1 0 3.969546e+16 0.0000000
Present_Price 0 0 -1.571441e+00 0.1171669
Kms_Driven 0 0 7.070469e-01 0.4801031
Fuel_TypeDiesel 0 0 1.722070e-02 0.9862723
Fuel_TypePetrol 0 0 -1.594567e-01 0.8734197
Seller_TypeIndividual 0 0 5.253902e-01 0.5997120
TransmissionManual 0 0 -6.726510e-01 0.5017031
Owner 0 0 -7.921000e-02 0.9369200
par(mfrow=c(2,2))
plot(full.model)

Here a residual analysis check is being performed, to ensure that the data meets the necessary conditions of normality.

my_model <- lm(price ~ year + Kms_Driven, data = car)

par(cex.main = 3.0)

par(mfrow = c(2, 2)) 
plot(my_model, which = 1) # Residual plot
plot(my_model, which = 2) # Normal Q-Q plot
plot(my_model, which = 3) # Scale-location plot
plot(my_model, which = 5) # Residuals vs leverage plot

In this case, the data is not considered normal and does not pass the conditions, so a boxcox transformation is needed in order to try and correct the data.

# Load the required packages
library(MASS)

# Set up plotting parameters
par(pty = "s", mfrow = c(2, 2), oma=c(.1,.1,.1,.1), mar=c(4, 0, 2, 0))

# Perform box-cox transformation for Price using predictor variables 
boxcox(price ~ year + Kms_Driven + Fuel_Type, data = car, lambda = seq(0, 1, length = 10), 
       xlab = expression(paste(lambda, ": Kms_Driven")))

boxcox(price ~ year + Kms_Driven + Fuel_Type, data = car, lambda = seq(-0.5, 1, length = 10), 
       xlab = expression(paste(lambda, ": Kms_Driven")))

boxcox(price ~ log(1 + year) + Kms_Driven + Fuel_Type, data = car, lambda = seq(-0.5, 1, length = 10), 
       xlab = expression(paste(lambda, ": log-year")))

First a square root transformation is performed, in this case the transformation has helped the Q-Q plot quite a bit. The residuals are now fitting better on the line then before.

library(kableExtra)
sqrt.price.log.dist <- lm((price)^0.5 ~ Year + Kms_Driven, 
                          data = car_data)

kable(summary(sqrt.price.log.dist)$coef, caption = "log-transformed model")
log-transformed model
Estimate Std. Error t value Pr(\>\|t\|)
(Intercept) -280.9101522 47.1333884 -5.959897 0.0000000
Year 0.1403272 0.0233904 5.999341 0.0000000
Kms_Driven 0.0000062 0.0000017 3.567700 0.0004195
par(mfrow = c(2,2))
plot(sqrt.price.log.dist)

A log transformation is performed below, and while it does help, it still falls behind the square root transformation.

log.price = lm(log(price) ~ year + Kms_Driven, data = car)
kable(summary(log.price)$coef, caption = "log-transformed model")
log-transformed model
Estimate Std. Error t value Pr(\>\|t\|)
(Intercept) -332.1779251 57.1164960 -5.815797 0.0000000
year 0.1652990 0.0283447 5.831751 0.0000000
Kms_Driven 0.0000065 0.0000021 3.065149 0.0023748
par(mfrow = c(2,2))
plot(log.price)

par(pty = "s", mfrow = c(1, 3))
#Q-Q plot for original model
qqnorm(full.model$residuals, main = "Full-Model")
qqline(full.model$residuals)
#Q-Q plot for Box-Cox transformed model
qqnorm(log.price$residuals, main = "Log-Price")
qqline(log.price$residuals)
#display both Q-Q plots
qqnorm(sqrt.price.log.dist$residuals, main = "sqrt price log dist")
qqline(sqrt.price.log.dist$residuals)

This displays the goodness of fit measurements for the three models, the full model, the square root transformation, and the log transformation. The full model has the highest R-squared value with a value of 1 a perfect fit, pottentianly indicated overfitting. The next best model presented is the square root transformation, but in this case, the r-squared value is relatively low at 0.1084. Meaning that 10.84% of the variability in the response variable price is explained by the predictor variables. The square root transformation also outperforms the log transformation in the fact that it has lower sum of squares(SSE), Akaike information criterion (AIC), and simulation-based calibration (SBC) values.

select=function(m){ 
 e = m$resid                           
 n0 = length(e)                        
 SSE=(m$df)*(summary(m)$sigma)^2       
 R.sq=summary(m)$r.squared             
 R.adj=summary(m)$adj.r                
 MSE=(summary(m)$sigma)^2              
 Cp=(SSE/MSE)-(n0-2*(n0-m$df))         
 AIC=n0*log(SSE)-n0*log(n0)+2*(n0-m$df)          
 SBC=n0*log(SSE)-n0*log(n0)+(log(n0))*(n0-m$df)  
 X=model.matrix(m)                     
 H=X%*%solve(t(X)%*%X)%*%t(X)         
 d=e/(1-diag(H))                       
 PRESS=t(d)%*%d   
 tbl = as.data.frame(cbind(SSE=SSE, R.sq=R.sq, R.adj = R.adj, Cp = Cp, AIC = AIC, SBC = SBC, PRD = PRESS))
 names(tbl)=c("SSE", "R.sq", "R.adj", "Cp", "AIC", "SBC", "PRESS")
 tbl
 }
output.sum = rbind(select(full.model), select(sqrt.price.log.dist), select(log.price))
## Warning in summary.lm(m): essentially perfect fit: summary may be unreliable

## Warning in summary.lm(m): essentially perfect fit: summary may be unreliable

## Warning in summary.lm(m): essentially perfect fit: summary may be unreliable

## Warning in summary.lm(m): essentially perfect fit: summary may be unreliable
row.names(output.sum) = c("full.model", "sqrt.price.log.dist", "log.price")
kable(output.sum, caption = "Goodness-of-fit Measures of Candidate Models")
Goodness-of-fit Measures of Candidate Models
SSE R.sq R.adj Cp AIC SBC PRESS
full.model 0.0000 1.0000000 1.0000000 10 -20947.735255 -20910.66415 0.0000
sqrt.price.log.dist 296.5199 0.1084189 0.1024352 3 1.486231 12.60756 354.2954
log.price 435.4313 0.1024350 0.0964111 3 117.137258 128.25859 537.6909

In this case both the year of the car and Kms_Driven variables are statistically significant predictors price as the corresponding t-values are large and the p-values are less than alpha = 0.05.The negative intercept coefficient suggests that the outcome variable is predicted to decrease as the independent variables increase.

kable(summary(log.price)$coef, caption = "Inferential Statistics of Final Model")
Inferential Statistics of Final Model
Estimate Std. Error t value Pr(\>\|t\|)
(Intercept) -332.1779251 57.1164960 -5.815797 0.0000000
year 0.1652990 0.0283447 5.831751 0.0000000
Kms_Driven 0.0000065 0.0000021 3.065149 0.0023748