Goal: to predict the rental prices in the SF rental market Click here for the data.

Import Data

rent <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/main/data/2022/2022-07-05/rent.csv')
## Rows: 200796 Columns: 17
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (8): post_id, nhood, city, county, address, title, descr, details
## dbl (9): date, year, price, beds, baths, sqft, room_in_apt, lat, lon
## 
## ℹ 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.
skimr::skim(rent)
Data summary
Name rent
Number of rows 200796
Number of columns 17
_______________________
Column type frequency:
character 8
numeric 9
________________________
Group variables None

Variable type: character

skim_variable n_missing complete_rate min max empty n_unique whitespace
post_id 0 1.00 9 14 0 200796 0
nhood 0 1.00 4 43 0 167 0
city 0 1.00 5 19 0 104 0
county 1394 0.99 4 13 0 10 0
address 196888 0.02 1 38 0 2869 0
title 2517 0.99 2 298 0 184961 0
descr 197542 0.02 13 16975 0 3025 0
details 192780 0.04 4 595 0 7667 0

Variable type: numeric

skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
date 0 1.00 20095718.38 44694.07 20000902.00 20050227.00 20110924.00 20120805.0 20180717.00 ▁▇▁▆▃
year 0 1.00 2009.51 4.48 2000.00 2005.00 2011.00 2012.0 2018.00 ▁▇▁▆▃
price 0 1.00 2135.36 1427.75 220.00 1295.00 1800.00 2505.0 40000.00 ▇▁▁▁▁
beds 6608 0.97 1.89 1.08 0.00 1.00 2.00 3.0 12.00 ▇▂▁▁▁
baths 158121 0.21 1.68 0.69 1.00 1.00 2.00 2.0 8.00 ▇▁▁▁▁
sqft 136117 0.32 1201.83 5000.22 80.00 750.00 1000.00 1360.0 900000.00 ▇▁▁▁▁
room_in_apt 0 1.00 0.00 0.04 0.00 0.00 0.00 0.0 1.00 ▇▁▁▁▁
lat 193145 0.04 37.67 0.35 33.57 37.40 37.76 37.8 40.43 ▁▁▅▇▁
lon 196484 0.02 -122.21 0.78 -123.20 -122.42 -122.26 -122.0 -74.20 ▇▁▁▁▁
data <- rent %>% 
    
    #Treat missing values
    select(-address, -descr, -details, -lat, -lon) %>%
    na.omit() %>%
    
    # log transform variables with pos-skewed distribution
    mutate(price = log(price))

Explore Data

Identify good predictors

data %>%
    ggplot(aes(price, sqft)) +
    scale_y_log10() +
    geom_point()

beds

data %>% 
    ggplot(aes(price, as.factor(beds))) +
    geom_boxplot()

title

data %>% 
    
    #tokenize title
    unnest_tokens(output = word, input = title) %>%
    
    #calculate avg rent per word
    group_by(word) %>%
    summarise(price = mean(price),
              n     = n ()) %>%
    ungroup () %>%
    
    filter(n > 10, str_detect(word, "\\d")) %>%
    slice_max(order_by = price, n = 20) %>%
    
    # Plot 
    ggplot(aes(price, fct_reorder(word, price))) +
    geom_point() +
    
    labs(y = "Words in Title")

EDA Shortcut

# Step 1: Prepare data
data_binarized_tbl <- data %>% 
    select(-post_id, -title) %>%
    binarize()

data_binarized_tbl %>% glimpse()
## Rows: 14,394
## Columns: 95
## $ `date__-Inf_20120214`                       <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ date__20120214_20140817                     <dbl> 1, 1, 1, 1, 0, 1, 1, 1, 0,…
## $ date__20140817_20160126                     <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ date__20160126_Inf                          <dbl> 0, 0, 0, 0, 1, 0, 0, 0, 1,…
## $ `year__-Inf_2012`                           <dbl> 0, 1, 1, 1, 0, 1, 1, 0, 0,…
## $ year__2012_2014                             <dbl> 1, 0, 0, 0, 0, 0, 0, 1, 0,…
## $ year__2014_2016                             <dbl> 0, 0, 0, 0, 1, 0, 0, 0, 1,…
## $ year__2016_Inf                              <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ nhood__campbell                             <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ `nhood__concord_/_pleasant_hill_/_martinez` <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ nhood__cupertino                            <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ nhood__daly_city                            <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ `nhood__danville_/_san_ramon`               <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ `nhood__dublin_/_pleasanton`                <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ `nhood__fairfield_/_vacaville`              <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ nhood__foster_city                          <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ `nhood__hayward_/_castro_valley`            <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ nhood__milpitas                             <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ nhood__mountain_view                        <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ nhood__napa_county                          <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ nhood__palo_alto                            <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ nhood__petaluma                             <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ `nhood__pittsburg_/_antioch`                <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ `nhood__rohnert_pk_/_cotati`                <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ nhood__san_francisco                        <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ nhood__san_jose_central                     <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ nhood__san_jose_east                        <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ nhood__san_jose_north                       <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ nhood__san_jose_south                       <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ nhood__san_jose_west                        <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ nhood__san_mateo                            <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ nhood__san_rafael                           <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ nhood__santa_clara                          <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ nhood__santa_cruz                           <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ nhood__santa_rosa                           <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ `nhood__SOMA_/_south_beach`                 <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ nhood__sunnyvale                            <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ nhood__union_city                           <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ `nhood__vallejo_/_benicia`                  <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ `nhood__willow_glen_/_cambrian`             <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ `nhood__-OTHER`                             <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1,…
## $ city__cambrian                              <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ city__campbell                              <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ city__concord                               <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ city__cupertino                             <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ city__daly_city                             <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ city__dublin                                <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ city__fairfield                             <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ city__foster_city                           <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ city__hayward                               <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ city__milpitas                              <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ city__mountain_view                         <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ city__napa_county                           <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ city__oakland                               <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ city__palo_alto                             <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ city__petaluma                              <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ city__pittsburg                             <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ city__rohnert_park                          <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ city__san_francisco                         <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ city__san_jose                              <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ city__san_mateo                             <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ city__san_rafael                            <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ city__san_ramon                             <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ city__santa_clara                           <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ city__santa_cruz                            <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ city__santa_rosa                            <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ city__sunnyvale                             <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ city__union_city                            <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ city__vallejo                               <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ `city__-OTHER`                              <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1,…
## $ county__alameda                             <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1,…
## $ county__contra_costa                        <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ county__marin                               <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ county__napa                                <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ county__san_francisco                       <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ county__san_mateo                           <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ county__santa_clara                         <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ county__santa_cruz                          <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ county__solano                              <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ county__sonoma                              <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ `price__-Inf_7.52294091807237`              <dbl> 0, 1, 0, 1, 0, 1, 1, 0, 0,…
## $ price__7.52294091807237_7.80384330353877    <dbl> 0, 0, 1, 0, 0, 0, 0, 1, 1,…
## $ price__7.80384330353877_8.07868822922987    <dbl> 1, 0, 0, 0, 1, 0, 0, 0, 0,…
## $ price__8.07868822922987_Inf                 <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ `beds__-Inf_2`                              <dbl> 0, 1, 0, 1, 1, 1, 0, 0, 1,…
## $ beds__2_3                                   <dbl> 0, 0, 1, 0, 0, 0, 1, 1, 0,…
## $ beds__3_Inf                                 <dbl> 1, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ `baths__-Inf_2`                             <dbl> 0, 1, 1, 1, 1, 1, 0, 0, 1,…
## $ baths__2_Inf                                <dbl> 1, 0, 0, 0, 0, 0, 1, 1, 0,…
## $ `sqft__-Inf_887`                            <dbl> 0, 1, 0, 0, 0, 0, 0, 0, 1,…
## $ sqft__887_1100                              <dbl> 0, 0, 0, 1, 0, 0, 0, 1, 0,…
## $ sqft__1100_1500                             <dbl> 0, 0, 1, 0, 1, 1, 0, 0, 0,…
## $ sqft__1500_Inf                              <dbl> 1, 0, 0, 0, 0, 0, 1, 0, 0,…
## $ room_in_apt__0                              <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1,…
## $ `room_in_apt__-OTHER`                       <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
# Step 2: Correlate
data_corr_tbl <- data_binarized_tbl %>%
    correlate(price__8.07868822922987_Inf)

data_corr_tbl
## # A tibble: 95 × 3
##    feature bin                               correlation
##    <fct>   <chr>                                   <dbl>
##  1 price   8.07868822922987_Inf                    1    
##  2 city    san_francisco                           0.389
##  3 county  san_francisco                           0.389
##  4 price   -Inf_7.52294091807237                  -0.342
##  5 price   7.80384330353877_8.07868822922987      -0.330
##  6 price   7.52294091807237_7.80384330353877      -0.328
##  7 sqft    1500_Inf                                0.324
##  8 beds    -Inf_2                                 -0.254
##  9 year    -Inf_2012                              -0.246
## 10 beds    3_Inf                                   0.241
## # ℹ 85 more rows
# Step 3: Plot
data_corr_tbl %>% 
    plot_correlation_funnel()
## Warning: ggrepel: 70 unlabeled data points (too many overlaps). Consider
## increasing max.overlaps

Build Models

Split Data

data <- sample_n(data, 100)

# split into train and test datasets
set.seed(1234)
data_split <- rsample::initial_split(data)
data_train <- training(data_split)
data_test  <- testing(data_split)

# further split training dataset for cross-validation
set.seed(2345)
data_cv <- rsample::vfold_cv(data_train)
data_cv
## #  10-fold cross-validation 
## # A tibble: 10 × 2
##    splits         id    
##    <list>         <chr> 
##  1 <split [67/8]> Fold01
##  2 <split [67/8]> Fold02
##  3 <split [67/8]> Fold03
##  4 <split [67/8]> Fold04
##  5 <split [67/8]> Fold05
##  6 <split [68/7]> Fold06
##  7 <split [68/7]> Fold07
##  8 <split [68/7]> Fold08
##  9 <split [68/7]> Fold09
## 10 <split [68/7]> Fold10
library(usemodels)
## Warning: package 'usemodels' was built under R version 4.4.2
usemodels::use_xgboost(price ~ ., data = data_train)
## xgboost_recipe <- 
##   recipe(formula = price ~ ., data = data_train) %>% 
##   step_zv(all_predictors()) 
## 
## xgboost_spec <- 
##   boost_tree(trees = tune(), min_n = tune(), tree_depth = tune(), learn_rate = tune(), 
##     loss_reduction = tune(), sample_size = tune()) %>% 
##   set_mode("classification") %>% 
##   set_engine("xgboost") 
## 
## xgboost_workflow <- 
##   workflow() %>% 
##   add_recipe(xgboost_recipe) %>% 
##   add_model(xgboost_spec) 
## 
## set.seed(6804)
## xgboost_tune <-
##   tune_grid(xgboost_workflow, resamples = stop("add your rsample object"), grid = stop("add number of candidate points"))
# specify recipe
xgboost_recipe <- 
    recipe(formula = price ~ ., data = data_train) %>%
    recipes::update_role(post_id, new_role = "id variable") %>%
    step_tokenize(title) %>%
    step_tokenfilter(title, max_tokens = 100) %>%
    step_tfidf(title) %>%
    step_other(nhood) %>%
    step_dummy(nhood, city, county, one_hot = TRUE) %>%
    step_YeoJohnson(sqft, beds, baths)

xgboost_recipe %>% prep() %>% juice() %>% glimpse()
## Rows: 75
## Columns: 156
## $ post_id                   <chr> "4834930575", "5418910487", "4656284087", "p…
## $ date                      <dbl> 20150106, 20160125, 20140907, 20120118, 2012…
## $ year                      <dbl> 2015, 2016, 2014, 2012, 2012, 2012, 2011, 20…
## $ beds                      <dbl> 0.7446282, 1.6025385, 1.6025385, 1.2316724, …
## $ baths                     <dbl> 0.5243879, 0.6374865, 0.7152871, 0.7152871, …
## $ sqft                      <dbl> 2.482040, 2.544714, 2.547805, 2.535112, 2.57…
## $ room_in_apt               <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ price                     <dbl> 6.956545, 8.101678, 7.279319, 7.762171, 7.95…
## $ tfidf_title_1             <dbl> 0.0000000, 0.0000000, 0.0000000, 0.0000000, …
## $ tfidf_title_1.5           <dbl> 0.0000000, 0.0000000, 0.0000000, 0.0000000, …
## $ tfidf_title_1100ft        <dbl> 0.0000000, 0.0000000, 0.0000000, 0.4056287, …
## $ tfidf_title_1150ft        <dbl> 0.0000000, 0.0000000, 0.0000000, 0.0000000, …
## $ tfidf_title_12            <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_1200ft        <dbl> 0.0000000, 0.0000000, 0.0000000, 0.0000000, …
## $ tfidf_title_1600          <dbl> 0.0000000, 0.0000000, 0.0000000, 0.0000000, …
## $ tfidf_title_1600ft        <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_1800          <dbl> 0.0000000, 0.0000000, 0.0000000, 0.0000000, …
## $ tfidf_title_1ba           <dbl> 0.0000000, 0.0000000, 0.0000000, 0.0000000, …
## $ tfidf_title_1bath         <dbl> 0.0000000, 0.0000000, 0.0000000, 0.0000000, …
## $ tfidf_title_1br           <dbl> 0.0000000, 0.0000000, 0.0000000, 0.0000000, …
## $ tfidf_title_2             <dbl> 0.00000000, 0.00000000, 0.24589038, 0.000000…
## $ tfidf_title_2.5           <dbl> 0.0000000, 0.0000000, 0.0000000, 0.0000000, …
## $ tfidf_title_2.5ba         <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_2200          <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_23            <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_2650          <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_2800          <dbl> 0.0000000, 0.0000000, 0.0000000, 0.0000000, …
## $ tfidf_title_2ba           <dbl> 0.0000000, 0.0000000, 0.0000000, 0.0000000, …
## $ tfidf_title_2bed          <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_2br           <dbl> 0.0000000, 0.0000000, 0.0000000, 0.1574518, …
## $ tfidf_title_3             <dbl> 0.0000000, 0.0000000, 0.4112904, 0.0000000, …
## $ tfidf_title_3200          <dbl> 0.0000000, 0.0000000, 0.0000000, 0.0000000, …
## $ tfidf_title_3ba           <dbl> 0.0000000, 0.0000000, 0.0000000, 0.0000000, …
## $ tfidf_title_3bd           <dbl> 0.0000000, 0.0000000, 0.0000000, 0.0000000, …
## $ tfidf_title_3br           <dbl> 0.0000000, 0.5660004, 0.0000000, 0.0000000, …
## $ tfidf_title_4             <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_4000          <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_4100          <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_4br           <dbl> 0.000000, 0.000000, 0.000000, 0.000000, 0.00…
## $ tfidf_title_5             <dbl> 0.0000000, 0.0000000, 0.0000000, 0.0000000, …
## $ tfidf_title_6br           <dbl> 0.000000, 0.000000, 0.000000, 0.000000, 0.00…
## $ tfidf_title_7             <dbl> 0.0000000, 0.0000000, 0.0000000, 0.0000000, …
## $ tfidf_title_700ft         <dbl> 0.000000, 0.000000, 0.000000, 0.000000, 0.00…
## $ tfidf_title_8             <dbl> 0.0000000, 0.0000000, 0.0000000, 0.0000000, …
## $ tfidf_title_9             <dbl> 0.0000000, 0.0000000, 0.0000000, 0.0000000, …
## $ tfidf_title_900ft         <dbl> 0.0000000, 0.0000000, 0.0000000, 0.0000000, …
## $ tfidf_title_a             <dbl> 0.0000000, 0.0000000, 0.0000000, 0.0000000, …
## $ tfidf_title_amazing       <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_amp           <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_and           <dbl> 0.0000000, 0.0000000, 0.0000000, 0.0000000, …
## $ tfidf_title_apartment     <dbl> 0.0000000, 0.0000000, 0.0000000, 0.0000000, …
## $ tfidf_title_at            <dbl> 0.0000000, 0.0000000, 0.5205379, 0.0000000, …
## $ tfidf_title_ba            <dbl> 0.0000000, 0.0000000, 0.0000000, 0.0000000, …
## $ tfidf_title_bath          <dbl> 0.0000000, 0.0000000, 0.0000000, 0.0000000, …
## $ tfidf_title_bathroom      <dbl> 0.0000000, 0.0000000, 0.4678798, 0.2599332, …
## $ tfidf_title_baths         <dbl> 0.0000000, 0.0000000, 0.0000000, 0.0000000, …
## $ tfidf_title_beautiful     <dbl> 0.0000000, 0.0000000, 0.0000000, 0.0000000, …
## $ tfidf_title_bed           <dbl> 0.0000000, 0.0000000, 0.0000000, 0.0000000, …
## $ tfidf_title_bedroom       <dbl> 0.9247895, 0.0000000, 0.3699158, 0.2055088, …
## $ tfidf_title_br            <dbl> 0.0000000, 0.0000000, 0.0000000, 0.0000000, …
## $ tfidf_title_bright        <dbl> 0.0000000, 0.0000000, 0.0000000, 0.0000000, …
## $ tfidf_title_condo         <dbl> 0.0000000, 0.0000000, 0.0000000, 0.0000000, …
## $ tfidf_title_den           <dbl> 0.0000000, 0.0000000, 0.0000000, 0.0000000, …
## $ tfidf_title_district      <dbl> 0.0000000, 0.0000000, 0.0000000, 0.0000000, …
## $ tfidf_title_dryer         <dbl> 0.0000000, 0.0000000, 0.0000000, 0.0000000, …
## $ tfidf_title_duplex        <dbl> 0.0000000, 0.0000000, 0.0000000, 0.0000000, …
## $ tfidf_title_flat          <dbl> 0.0000000, 0.0000000, 0.0000000, 0.0000000, …
## $ tfidf_title_floor         <dbl> 0.0000000, 0.0000000, 0.0000000, 0.0000000, …
## $ tfidf_title_for           <dbl> 0.0000000, 0.0000000, 0.0000000, 0.0000000, …
## $ tfidf_title_garage        <dbl> 0.0000000, 0.0000000, 0.0000000, 0.0000000, …
## $ tfidf_title_great         <dbl> 0.0000000, 0.0000000, 0.0000000, 0.0000000, …
## $ tfidf_title_har           <dbl> 0.0000000, 0.4261648, 0.0000000, 0.0000000, …
## $ tfidf_title_hide          <dbl> 0.0000000, 0.0000000, 0.0000000, 0.0000000, …
## $ tfidf_title_home          <dbl> 0.0000000, 0.2830002, 0.0000000, 0.0000000, …
## $ tfidf_title_house         <dbl> 0.0000000, 0.0000000, 0.0000000, 0.2377851, …
## $ tfidf_title_in            <dbl> 0.0000000, 0.0000000, 0.0000000, 0.0000000, …
## $ tfidf_title_large         <dbl> 0.0000000, 0.4261648, 0.0000000, 0.0000000, …
## $ tfidf_title_location      <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_loft          <dbl> 0.0000000, 0.0000000, 0.0000000, 0.0000000, …
## $ tfidf_title_luxury        <dbl> 0.0000000, 0.0000000, 0.0000000, 0.3080654, …
## $ tfidf_title_map           <dbl> 0.000000, 0.000000, 0.000000, 0.000000, 0.00…
## $ tfidf_title_mission       <dbl> 0.0000000, 0.0000000, 0.0000000, 0.0000000, …
## $ tfidf_title_modern        <dbl> 0.0000000, 0.0000000, 0.0000000, 0.0000000, …
## $ tfidf_title_new           <dbl> 0.0000000, 0.0000000, 0.0000000, 0.0000000, …
## $ tfidf_title_newly         <dbl> 0.0000000, 0.0000000, 0.0000000, 0.0000000, …
## $ tfidf_title_one           <dbl> 1.629048, 0.000000, 0.000000, 0.000000, 0.00…
## $ tfidf_title_pic           <dbl> 0.0000000, 0.0000000, 0.0000000, 0.0000000, …
## $ tfidf_title_pleasanton    <dbl> 0.0000000, 0.0000000, 0.0000000, 0.0000000, …
## $ tfidf_title_pool          <dbl> 0.0000000, 0.0000000, 0.0000000, 0.0000000, …
## $ tfidf_title_posting       <dbl> 0.0000000, 0.0000000, 0.0000000, 0.0000000, …
## $ tfidf_title_private       <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_remodeled     <dbl> 0.0000000, 0.0000000, 0.0000000, 0.0000000, …
## $ tfidf_title_rent          <dbl> 0.0000000, 0.0000000, 0.0000000, 0.0000000, …
## $ tfidf_title_resim         <dbl> 0.0000000, 0.4261648, 0.0000000, 0.0000000, …
## $ tfidf_title_restore       <dbl> 0.0000000, 0.0000000, 0.0000000, 0.0000000, …
## $ tfidf_title_san           <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_spacious      <dbl> 0.0000000, 0.0000000, 0.0000000, 0.0000000, …
## $ tfidf_title_this          <dbl> 0.0000000, 0.0000000, 0.0000000, 0.0000000, …
## $ tfidf_title_to            <dbl> 0.0000000, 0.0000000, 0.0000000, 0.0000000, …
## $ tfidf_title_two           <dbl> 0.0000000, 0.0000000, 0.0000000, 0.7240215, …
## $ tfidf_title_unit          <dbl> 0.0000000, 0.0000000, 0.0000000, 0.0000000, …
## $ tfidf_title_view          <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_w             <dbl> 0.0000000, 0.0000000, 0.0000000, 0.0000000, …
## $ tfidf_title_washer        <dbl> 0.0000000, 0.0000000, 0.0000000, 0.0000000, …
## $ tfidf_title_with          <dbl> 0.0000000, 0.2937789, 0.0000000, 0.2284947, …
## $ tfidf_title_사진          <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_월            <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_지도          <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ nhood_dublin...pleasanton <dbl> 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0,…
## $ nhood_mountain.view       <dbl> 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0,…
## $ nhood_other               <dbl> 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1,…
## $ city_belmont              <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ city_brentwood            <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ city_burlingame           <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ city_cambrian             <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ city_concord              <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ city_daly.city            <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ city_dublin               <dbl> 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0,…
## $ city_el.cerrito           <dbl> 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ city_fairfax              <dbl> 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ city_fairfield            <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,…
## $ city_hayward              <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,…
## $ city_menlo.park           <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ city_milpitas             <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ city_mountain.view        <dbl> 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0,…
## $ city_napa.county          <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,…
## $ city_oakland              <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ city_orinda               <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ city_pacifica             <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ city_palo.alto            <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ city_pittsburg            <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ city_richmond             <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ city_san.bruno            <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ city_san.francisco        <dbl> 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0,…
## $ city_san.jose             <dbl> 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,…
## $ city_san.mateo            <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ city_san.rafael           <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ city_san.ramon            <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ city_santa.clara          <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ city_santa.cruz           <dbl> 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ city_santa.rosa           <dbl> 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ city_saratoga             <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ city_sausalito            <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ city_sunnyvale            <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ city_union.city           <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ city_vallejo              <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ county_alameda            <dbl> 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0,…
## $ county_contra.costa       <dbl> 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ county_marin              <dbl> 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ county_napa               <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,…
## $ county_san.francisco      <dbl> 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0,…
## $ county_san.mateo          <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ county_santa.clara        <dbl> 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0,…
## $ county_santa.cruz         <dbl> 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ county_solano             <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,…
## $ county_sonoma             <dbl> 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
# specify model
xgboost_spec <- 
  boost_tree(trees = tune(), min_n = tune(), mtry = tune (), learn_rate = tune()) %>% 
  set_mode("regression") %>% 
  set_engine("xgboost") 

# combine recipe and model using workflow
xgboost_workflow <- 
  workflow() %>% 
  add_recipe(xgboost_recipe) %>% 
  add_model(xgboost_spec) 

# tune hyperparameters
set.seed(344)
xgboost_tune <-
  tune_grid(xgboost_workflow, 
            resamples = data_cv,
            grid = 5)
## i Creating pre-processing data to finalize unknown parameter: mtry
## Warning: package 'xgboost' was built under R version 4.4.2
## → A | warning: ! There are new levels in `city`: "hayward" and "san bruno".
##                ℹ Consider using step_novel() (`?recipes::step_novel()`) before `step_dummy()`
##                  to handle unseen values.
## There were issues with some computations   A: x1There were issues with some computations   A: x2There were issues with some computations   A: x3There were issues with some computations   A: x4There were issues with some computations   A: x5                                                 → B | warning: A correlation computation is required, but `estimate` is constant and has 0
##                standard deviation, resulting in a divide by 0 error. `NA` will be returned.
## There were issues with some computations   A: x5There were issues with some computations   A: x5   B: x1                                                         → C | warning: ! There are new levels in `city`: "santa cruz" and "union city".
##                ℹ Consider using step_novel() (`?recipes::step_novel()`) before `step_dummy()`
##                  to handle unseen values., ! There are new levels in `county`: "santa cruz".
##                ℹ Consider using step_novel() (`?recipes::step_novel()`) before `step_dummy()`
##                  to handle unseen values.
## There were issues with some computations   A: x5   B: x1There were issues with some computations   A: x5   B: x1   C: x1There were issues with some computations   A: x5   B: x1   C: x2There were issues with some computations   A: x5   B: x1   C: x3There were issues with some computations   A: x5   B: x1   C: x4There were issues with some computations   A: x5   B: x1   C: x5                                                                 → D | warning: ! There are new levels in `city`: "cambrian".
##                ℹ Consider using step_novel() (`?recipes::step_novel()`) before `step_dummy()`
##                  to handle unseen values.
## There were issues with some computations   A: x5   B: x1   C: x5There were issues with some computations   A: x5   B: x2   C: x5   D: x1There were issues with some computations   A: x5   B: x2   C: x5   D: x2There were issues with some computations   A: x5   B: x2   C: x5   D: x3There were issues with some computations   A: x5   B: x2   C: x5   D: x4There were issues with some computations   A: x5   B: x2   C: x5   D: x5                                                                         → E | warning: ! There are new levels in `city`: "santa rosa" and "menlo park".
##                ℹ Consider using step_novel() (`?recipes::step_novel()`) before `step_dummy()`
##                  to handle unseen values., ! There are new levels in `county`: "sonoma".
##                ℹ Consider using step_novel() (`?recipes::step_novel()`) before `step_dummy()`
##                  to handle unseen values.
## There were issues with some computations   A: x5   B: x2   C: x5   D: x5There were issues with some computations   A: x5   B: x3   C: x5   D: x5   E: x1There were issues with some computations   A: x5   B: x3   C: x5   D: x5   E: x2There were issues with some computations   A: x5   B: x3   C: x5   D: x5   E: x3There were issues with some computations   A: x5   B: x3   C: x5   D: x5   E: x4There were issues with some computations   A: x5   B: x3   C: x5   D: x5   E: x5There were issues with some computations   A: x5   B: x4   C: x5   D: x5   E: x5                                                                                 → F | warning: ! There are new levels in `city`: "el cerrito" and "belmont".
##                ℹ Consider using step_novel() (`?recipes::step_novel()`) before `step_dummy()`
##                  to handle unseen values.
## There were issues with some computations   A: x5   B: x4   C: x5   D: x5   E: x5There were issues with some computations   A: x5   B: x4   C: x5   D: x5   E: x…There were issues with some computations   A: x5   B: x4   C: x5   D: x5   E: x…There were issues with some computations   A: x5   B: x4   C: x5   D: x5   E: x…There were issues with some computations   A: x5   B: x4   C: x5   D: x5   E: x…There were issues with some computations   A: x5   B: x4   C: x5   D: x5   E: x…                                                                                 → G | warning: ! There are new levels in `city`: "orinda".
##                ℹ Consider using step_novel() (`?recipes::step_novel()`) before `step_dummy()`
##                  to handle unseen values.
## There were issues with some computations   A: x5   B: x4   C: x5   D: x5   E: x…There were issues with some computations   A: x5   B: x5   C: x5   D: x5   E: x…There were issues with some computations   A: x5   B: x5   C: x5   D: x5   E: x…There were issues with some computations   A: x5   B: x5   C: x5   D: x5   E: x…There were issues with some computations   A: x5   B: x5   C: x5   D: x5   E: x…There were issues with some computations   A: x5   B: x5   C: x5   D: x5   E: x…                                                                                 → H | warning: ! There are new levels in `city`: "pacifica".
##                ℹ Consider using step_novel() (`?recipes::step_novel()`) before `step_dummy()`
##                  to handle unseen values.
## There were issues with some computations   A: x5   B: x5   C: x5   D: x5   E: x…There were issues with some computations   A: x5   B: x6   C: x5   D: x5   E: x…There were issues with some computations   A: x5   B: x6   C: x5   D: x5   E: x…There were issues with some computations   A: x5   B: x6   C: x5   D: x5   E: x…There were issues with some computations   A: x5   B: x6   C: x5   D: x5   E: x…There were issues with some computations   A: x5   B: x6   C: x5   D: x5   E: x…                                                                                 → I | warning: ! There are new levels in `city`: "napa county", "santa clara", "richmond",
##                  "burlingame", and "brentwood".
##                ℹ Consider using step_novel() (`?recipes::step_novel()`) before `step_dummy()`
##                  to handle unseen values., ! There are new levels in `county`: "napa".
##                ℹ Consider using step_novel() (`?recipes::step_novel()`) before `step_dummy()`
##                  to handle unseen values.
## There were issues with some computations   A: x5   B: x6   C: x5   D: x5   E: x…There were issues with some computations   A: x5   B: x7   C: x5   D: x5   E: x…There were issues with some computations   A: x5   B: x7   C: x5   D: x5   E: x…There were issues with some computations   A: x5   B: x7   C: x5   D: x5   E: x…There were issues with some computations   A: x5   B: x7   C: x5   D: x5   E: x…There were issues with some computations   A: x5   B: x7   C: x5   D: x5   E: x…                                                                                 → J | warning: ! There are new levels in `city`: "sausalito" and "vallejo".
##                 ℹ Consider using step_novel() (`?recipes::step_novel()`) before `step_dummy()`
##                   to handle unseen values.
## There were issues with some computations   A: x5   B: x7   C: x5   D: x5   E: x…There were issues with some computations   A: x5   B: x8   C: x5   D: x5   E: x…There were issues with some computations   A: x5   B: x8   C: x5   D: x5   E: x…There were issues with some computations   A: x5   B: x8   C: x5   D: x5   E: x…There were issues with some computations   A: x5   B: x8   C: x5   D: x5   E: x…There were issues with some computations   A: x5   B: x8   C: x5   D: x5   E: x…There were issues with some computations   A: x5   B: x9   C: x5   D: x5   E: x…                                                                                 → K | warning: ! There are new levels in `city`: "concord", "san ramon", and "saratoga".
##                 ℹ Consider using step_novel() (`?recipes::step_novel()`) before `step_dummy()`
##                   to handle unseen values.
## There were issues with some computations   A: x5   B: x9   C: x5   D: x5   E: x…There were issues with some computations   A: x5   B: x9   C: x5   D: x5   E: x…There were issues with some computations   A: x5   B: x9   C: x5   D: x5   E: x…There were issues with some computations   A: x5   B: x9   C: x5   D: x5   E: x…There were issues with some computations   A: x5   B: x9   C: x5   D: x5   E: x…There were issues with some computations   A: x5   B: x9   C: x5   D: x5   E: x…There were issues with some computations   A: x5   B: x10   C: x5   D: x5   E: …

Evaluate Models

tune::show_best(xgboost_tune, metric = "rmse")
## # A tibble: 5 × 10
##    mtry trees min_n learn_rate .metric .estimator  mean     n std_err .config   
##   <int> <int> <int>      <dbl> <chr>   <chr>      <dbl> <int>   <dbl> <chr>     
## 1   122  1524    23    0.0836  rmse    standard   0.385    10  0.0451 Preproces…
## 2    49  1104    28    0.00484 rmse    standard   0.393    10  0.0436 Preproces…
## 3    92   768    12    0.112   rmse    standard   0.449    10  0.0545 Preproces…
## 4    10  1613    36    0.0290  rmse    standard   0.479    10  0.0434 Preproces…
## 5   138   162     7    0.00108 rmse    standard   6.16     10  0.0544 Preproces…
# Update the model by selecting the best hyperparameters
xgboost_fw <- tune::finalize_workflow(xgboost_workflow,
                        tune::select_best(xgboost_tune, metric = "rmse"))

# fit the model on the entire training data and test it on the test data.
data_fit <- tune::last_fit(xgboost_fw, data_split)
## → A | warning: ! There are new levels in `city`: "foster city", "campbell", "petaluma", and
##                  "soquel".
##                ℹ Consider using step_novel() (`?recipes::step_novel()`) before `step_dummy()`
##                  to handle unseen values.
## There were issues with some computations   A: x1There were issues with some computations   A: x1
tune::collect_metrics(data_fit)
## # A tibble: 2 × 4
##   .metric .estimator .estimate .config             
##   <chr>   <chr>          <dbl> <chr>               
## 1 rmse    standard       0.358 Preprocessor1_Model1
## 2 rsq     standard       0.295 Preprocessor1_Model1
tune::collect_predictions(data_fit) %>%
    ggplot(aes(price, .pred)) +
    geom_point(alpha = 0.3, fill = "midnightblue") +
    geom_abline(lty = 2, color = "gray50") +
    coord_fixed()

Make Predictions