Last active
January 19, 2021 14:36
-
-
Save sofianhamiti/e2299527377d55dfe4ce0a688634d9fc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library(randomForest) | |
| library(MASS) | |
| set.seed(42) | |
| # Prepare dataset | |
| cutoff = sample(1:nrow(Boston), nrow(Boston) * 0.7) # 70/30 split between test and train | |
| train_set <- Boston[cutoff,] | |
| test_set <- Boston[-cutoff,] | |
| # Train model | |
| rf_model = randomForest(medv ~ ., data = train_set, mtry = 6, importance = TRUE) | |
| # Evaluate model (MSE) | |
| boston.test = Boston[-cutoff, 'medv'] | |
| predictions = predict(rf_model, newdata = test_set) | |
| round(mean((predictions - boston.test)^2), 2) | |
| # Save model binary | |
| saveRDS(rf_model, file = 'boston_model.rds') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment