Projects / NYC housing models
Predicting New York house prices in R
A staged R pipeline that cleans 1,734 residential sales, picks predictors with stepwise AIC, and compares a linear model against a random forest under k-fold cross-validation.
How it's built
The pipeline runs in ordered stages: ingest, clean, transform, model, report. Each stage can be rerun on its own, which made it easy to change a cleaning rule and see what it did to the models without rerunning everything by hand.
Two models, one test
I started with a linear model on every candidate predictor and let stepwise AIC drop the ones that weren't pulling their weight, which left 7. Then I trained a random forest on the same data and compared both with k-fold cross-validation, so neither model got to look good on one lucky split.
# model comparison after the cleaning and transform stages full_model <- lm(price ~ ., data = train_clean) step_model <- step(full_model, direction = "both", trace = 0) rf_model <- randomForest(price ~ ., data = train_clean, ntree = 500, importance = TRUE) # compare on held-out RMSE rmse(predict(step_model, test_clean), test_clean$price) rmse(predict(rf_model, test_clean), test_clean$price)
Results
- The best model explains 64% of the variance in price (R² 0.64)
- Living area and land value were the strongest signals by a long way
- The final report is a Quarto document with an executive summary, method notes and charts
0.64 is an honest number for this data. House prices depend on plenty that isn't in the dataset, like condition and the exact street, so I'd be suspicious of anything much higher without new features.
What I'd change
- Model log price instead of price, since the errors grow with the value of the house
- Report RMSE in dollars alongside R², because that's what someone pricing a house actually cares about
- Wrap the stages in a
targetspipeline so only the stages whose inputs changed get rerun