Queer European MD passionate about IT

Mission487Solutions.Rmd 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. ---
  2. title: 'Predicting Car Prices: Guided Project Solutions'
  3. output: html_document
  4. ---
  5. # Introduction to the data
  6. ```{r, message = FALSE, warning = FALSE }
  7. library(readr)
  8. library(tidyr)
  9. library(dplyr)
  10. cars <- read.csv("./data/imports-85.data")
  11. # Fixing the column names since the .data file reads headers incorrectly
  12. colnames(cars) <- c(
  13. "symboling",
  14. "normalized_losses",
  15. "make",
  16. "fuel_type",
  17. "aspiration",
  18. "num_doors",
  19. "body_style",
  20. "drive_wheels",
  21. "engine_location",
  22. "wheel_base",
  23. "length",
  24. "width",
  25. "height",
  26. "curb_weight",
  27. "engine_type",
  28. "num_cylinders",
  29. "engine_size",
  30. "fuel_system",
  31. "bore",
  32. "stroke",
  33. "compression_ratio",
  34. "horsepower",
  35. "peak_rpm",
  36. "city_mpg",
  37. "highway_mpg",
  38. "price"
  39. )
  40. # Removing non-numerical columns and removing missing data
  41. cars <- cars %>%
  42. select(
  43. symboling, wheel_base, length, width, height, curb_weight,
  44. engine_size, bore, stroke, compression_ratio, horsepower,
  45. peak_rpm, city_mpg, highway_mpg, price
  46. ) %>%
  47. filter(
  48. stroke != "?",
  49. bore != "?",
  50. horsepower != "?",
  51. peak_rpm != "?",
  52. price != "?"
  53. ) %>%
  54. mutate(
  55. stroke = as.numeric(stroke),
  56. bore = as.numeric(bore),
  57. horsepower = as.numeric(horsepower),
  58. peak_rpm = as.numeric(peak_rpm),
  59. price = as.numeric(price)
  60. )
  61. # Confirming that each of the columns are numeric
  62. library(purrr)
  63. map(cars, typeof)
  64. ```
  65. # Examining Relationships Between Predictors
  66. ```{r}
  67. library(caret)
  68. featurePlot(cars, cars$price)
  69. ```
  70. There looks to be a somewhat positive relationship between horsepower and price. City MPG and highway MPG look positive too, but there's a curious grouping that looks like it pops up. Many features look like they plateau in terms of price (ie even as we increase, price does not increase). Height seems not to have any meaningful relationship with price since the dots look like an evenly scattered plot.
  71. ```{r}
  72. library(ggplot2)
  73. ggplot(cars, aes(x = price)) +
  74. geom_histogram(color = "red") +
  75. labs(
  76. title = "Distribution of prices in cars dataset",
  77. x = "Price",
  78. y = "Frequency"
  79. )
  80. ```
  81. It looks like there's a reasonably even distirbution of the prices in the dataset, so there are no outliers. There are 2 cars whose price is zero, so this might be suspect. This only represents 1% of the entire dataset, so it shouldn't have too much impact on predictions, especially if we use a high number of neighbors.
  82. # Setting up the train-test split
  83. ```{r}
  84. library(caret)
  85. split_indices <- createDataPartition(cars$price, p = 0.8, list = FALSE)
  86. train_cars <- cars[split_indices,]
  87. test_cars <- cars[-split_indices,]
  88. ```
  89. # Cross-validation and hyperparameter optimization
  90. ```{r}
  91. # 5-fold cross-validation
  92. five_fold_control <- trainControl(method = "cv", number = 5)
  93. tuning_grid <- expand.grid(k = 1:20)
  94. ```
  95. # Choosing a model
  96. ```{r}
  97. # Creating a model based on all the features
  98. full_model <- train(price ~ .,
  99. data = train_cars,
  100. method = "knn",
  101. trControl = five_fold_control,
  102. tuneGrid = tuning_grid,
  103. preProcess = c("center", "scale"))
  104. ```
  105. # Final model evaluation
  106. ```{r}
  107. predictions <- predict(full_model, newdata = test_cars)
  108. postResample(pred = predictions, obs = test_cars$price)
  109. ```