Kaggle House Prices competition entry using R and tidymodels. Predicts sale prices of residential homes in Ames, Iowa using a random forest model.
- Algorithm: Random forest (
rangerengine viatidymodels) - Score: 0.14541 (rank 2124)
- API: Plumber service deployed on Google Cloud Run
- Live docs: https://ames-api-576662713224.europe-west12.run.app/__docs__/
| 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 |
|
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 |
Dependencies are managed by renv. Restore the environment:
renv::restore()Or install packages from scratch:
source("install-packages.R")Rscript ames_training.RThis will:
- Load and preprocess training data
- Fit a random forest model using 10-fold CV with 5 repeats
- Pin the model to
board/(local) andames-r-model(GCS) - Generate a submission CSV to
output/rf_submission.csv
- 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,YrSoldare treated as categorical - Remaining numeric NAs are KNN-imputed
Rscript run_api_local.RThe 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.
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-472418The service account needs roles/storage.objectAdmin on the bucket ames-r-model.
POST /predict— send feature data, get price predictionGET /__docs__— interactive API documentation (viarapidoc)
The plumber API authenticates to Google Cloud Storage to read the pinned model:
- If
ames-housing-472418-0f31c1a0322f.jsonexists locally → uses that key directly - Otherwise → falls back to Application Default Credentials (Cloud Run environment)
The JSON key file is gitignored.
test_API.Ris stale and references a Titanic model. Do not use it as-is.- The model object is
rangerviatidymodels; inference requiresparsnip,recipes,workflows, andranger. - R version: 4.5.1 (via
rocker/r-ver:4.5.1) - For development workflow and commands, see AGENTS.md.