Skip to content

Repository files navigation

Ames Housing

Kaggle House Prices competition entry using R and tidymodels. Predicts sale prices of residential homes in Ames, Iowa using a random forest model.

Structure

Path Purpose
ames_training.R Full pipeline: trains model, pins to local board and GCS, writes submission CSV
ames_tidymodels_v1.R Earlier version of the training script (no vetiver/GCS)
plumber.R API entrypoint used by Dockerfile; loads model from GCS bucket
vetiver_plumber.R Alternate generated plumber file (not used in production)
deploy.R Helper to pin a trained model to the local board
run_api_local.R Starts the Plumber API on localhost:8080
Dockerfile Container definition for Cloud Run deployment
gcloud.txt Deployment commands (build, tag, push, gcloud run deploy)
test_API.R ⚠️ Outdated — references Titanic model/URL, not Ames
input/ Kaggle data (train.csv, test.csv, data_description.txt)
output/ Trained model objects (.rds) and submission CSVs
board/ Local pins board for versioned model storage
renv/ R package environment managed by renv

Setup

Dependencies are managed by renv. Restore the environment:

renv::restore()

Or install packages from scratch:

source("install-packages.R")

Training

Rscript ames_training.R

This will:

  1. Load and preprocess training data
  2. Fit a random forest model using 10-fold CV with 5 repeats
  3. Pin the model to board/ (local) and ames-r-model (GCS)
  4. Generate a submission CSV to output/rf_submission.csv

Key preprocessing

  • SalePrice is log10-transformed during training; predictions are reversed with 10^.pred
  • Categorical columns with missing values (e.g. Alley, BsmtQual) get "None" as a fill level
  • Numeric columns MSSubClass, MoSold, YrSold are treated as categorical
  • Remaining numeric NAs are KNN-imputed

Running the API locally

Rscript run_api_local.R

The API serves at http://localhost:8080. It loads the model from the GCS bucket ames-r-model using the vctrs/vetiver stack. If a GCS service account JSON key is present at the repo root, it's used for auth; otherwise Application Default Credentials are used.

Deployment

The service runs on Google Cloud Run. Deploy with:

docker build -t ames-api:gcs -f Dockerfile .
docker tag ames-api:gcs europe-west12-docker.pkg.dev/ames-housing-472418/docker-repo/ames-api:gcs
docker push europe-west12-docker.pkg.dev/ames-housing-472418/docker-repo/ames-api:gcs

gcloud run deploy ames-api \
  --image=europe-west12-docker.pkg.dev/ames-housing-472418/docker-repo/ames-api:gcs \
  --region=europe-west12 \
  --platform=managed \
  --service-account=gcs-read-write@ames-housing-472418.iam.gserviceaccount.com \
  --allow-unauthenticated \
  --project=ames-housing-472418

The service account needs roles/storage.objectAdmin on the bucket ames-r-model.

API endpoints

  • POST /predict — send feature data, get price prediction
  • GET /__docs__ — interactive API documentation (via rapidoc)

GCS auth

The plumber API authenticates to Google Cloud Storage to read the pinned model:

  1. If ames-housing-472418-0f31c1a0322f.json exists locally → uses that key directly
  2. Otherwise → falls back to Application Default Credentials (Cloud Run environment)

The JSON key file is gitignored.

Notes

  • test_API.R is stale and references a Titanic model. Do not use it as-is.
  • The model object is ranger via tidymodels; inference requires parsnip, recipes, workflows, and ranger.
  • R version: 4.5.1 (via rocker/r-ver:4.5.1)
  • For development workflow and commands, see AGENTS.md.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages