Read the scraped data from bilweb.se and remove outliers with fewer than 10 cases (e.g. ethanol engines). Most data is categorical including horsepower
dtx <- read_rds("~/dtx.rds")
min_case_count <- function(x, n = 10) {
table(x)[x] >= n
}
cars <- dtx %>% bind_rows() %>% distinct() %>%
mutate(
Price = as.numeric(Price),
NOwners = as.integer(NOwners),
Year = factor(Year),
Mil = as.numeric(Mil),
Horsepower = factor(Horsepower),
Drive = factor(Drive),
Type = factor(Type),
Fuel = factor(Fuel)
) %>%
filter(across(where(is.factor), min_case_count)) %>%
mutate(across(where(is.factor), droplevels)) %>%
filter(
! (Price > 450000 | Price < 10000)
)
datatable(cars)
We build a random forest model, splitting training and testing data 90/10 due to the low number of observations
cars_split <- initial_split(cars, strata = "Price", prop = 0.9)
train_data <- training(cars_split)
testing_data <- testing(cars_split)
model <- rand_forest(mode = "regression", mtry = 3, trees = 1000) %>%
set_engine("ranger") %>%
fit(
log10(Price) ~ Type + Drive + NOwners + Year + Fuel + Mil,
data = train_data
)
model
## parsnip model object
##
## Ranger result
##
## Call:
## ranger::ranger(x = maybe_data_frame(x), y = y, mtry = min_cols(~3, x), num.trees = ~1000, num.threads = 1, verbose = FALSE, seed = sample.int(10^5, 1))
##
## Type: Regression
## Number of trees: 1000
## Sample size: 322
## Number of independent variables: 6
## Mtry: 3
## Target node size: 5
## Variable importance mode: none
## Splitrule: variance
## OOB prediction error (MSE): 0.002110047
## R squared (OOB): 0.8848571
Now test the model based on the test cases
results <- testing_data %>%
select(Price) %>%
mutate(Price = log10(Price)) %>%
bind_cols(
predict(
model,
new_data = testing_data[, c("Type","Drive", "NOwners", "Year", "Fuel", "Mil")]
)
)
mets <- results %>% metrics(truth = Price, estimate = .pred)
datatable(mets)
Fantastic values of rmse: 0.0528389
Testing ground truth price vs. predicted
ggplot(results, aes(x = .pred, y = Price)) +
geom_smooth() +
geom_point()
Now we retrain with the full data set
model <- rand_forest(mode = "regression", mtry = 3, trees = 1000) %>%
set_engine("ranger") %>%
fit(
log10(Price) ~ Type + Drive + NOwners + Year + Fuel + Mil,
data = cars
)
We create a data point for the car of interest and predict the value using the RF model
p <- tibble(
Type = factor("Halvkombi", levels = levels(cars$Type)),
Drive = factor("Manuell", levels = levels(cars$Drive)),
NOwners = 3,
Year = factor("2015", levels = levels(cars$Year)),
Fuel = factor("Diesel", levels = levels(cars$Fuel)),
Mil = 9200,
Horsepower = factor("110", levels = levels(cars$Horsepower))
)
p$Price <- 10^predict(model, p)$.pred
datatable(p)
The red cross marks our car of interest with the predicted price from the model.
The car is estimated here what looks like on the lower end, but both Diesel engines and half-combis are generally lower in value, so that’s expected.
ggplot(cars, aes(x = Mil, y = Price)) +
geom_smooth() +
geom_point() +
geom_hline(aes(yintercept = Price), data = p, col = "red", lty = 2) +
geom_vline(aes(xintercept = Mil), data = p, col = "red", lty = 2) +
scale_y_continuous(labels = comma) +
ggtitle("Mil")
We can tease out the strata here and see we are more or less in line with other offers.
ggplot(cars, aes(x = Mil, y = Price)) +
geom_point() +
facet_grid(Fuel ~ Type) +
geom_hline(aes(yintercept = Price), data = p, col = "red", lty = 2) +
geom_vline(aes(xintercept = Mil), data = p, col = "red", lty = 2) +
scale_y_continuous(labels = comma) +
ggtitle("Mil")
The car was sold to us as a 2016 but is actually built in late 2015. No matter, not much of a difference maker
ggplot(cars, aes(x = Year, y = Price)) +
geom_violin() +
geom_quasirandom() +
geom_hline(aes(yintercept = Price), data = p, col = "red", lty = 2) +
geom_vline(aes(xintercept = Year), data = p, col = "red", lty = 2) +
scale_y_continuous(labels = comma) +
ggtitle("Year")
Manual drivetrains sell for less in general. We are well within expectations
ggplot(cars, aes(x = Drive, y = Price)) +
geom_violin() +
geom_quasirandom() +
geom_hline(aes(yintercept = Price), data = p, col = "red", lty = 2) +
geom_vline(aes(xintercept = Drive), data = p, col = "red", lty = 2) +
scale_y_continuous(labels = comma) +
ggtitle("Drive")
As above, the half-combis are lower in price
ggplot(cars, aes(x = Type, y = Price)) +
geom_violin() +
geom_quasirandom() +
geom_hline(aes(yintercept = Price), data = p, col = "red", lty = 2) +
geom_vline(aes(xintercept = Drive), data = p, col = "red", lty = 2) +
scale_y_continuous(labels = comma) +
ggtitle("Type")
With current oil prices, Diesel cars are not exciting, so fetch less on the market
ggplot(cars, aes(x = Fuel, y = Price)) +
geom_violin() +
geom_point() +
geom_vline(xintercept = p$Fuel, col = "red", lty = 2) +
geom_hline(yintercept = p$Price, col = "red", lty = 2) +
scale_y_continuous(labels = comma) +
ggtitle("Fuel")
All in all, a final price of 1.519788^{5} may seem high, but lies within market expectations.