Homework 10
Before you begin
Homework 10 contains three questions on random-forest simulation, classification, and skill design. It is evaluated as Complete or Incomplete. For Questions 1 and 2, use either R or Python. You are not expected to use both languages.
Attempt each question before asking an AI tool for help. You may use AI tools, but you are responsible for checking the work and being able to explain your calculations and plots.
Download the materials
Solutions for Questions 1 and 2 are included in the combined ZIP. No solution will be provided for Question 3.
Use the RLT package for Questions 1 and 2: RLT in R or rlt in Python. The node-size parameter is nmin in R and min_samples_leaf in Python; both stop splitting a node when it contains at most that many observations. The candidate-predictor parameter is mtry in both languages. Use ordinary random forests, with reinforcement turned off.
Question 1: Bias-variance trade-off simulation
Two important tuning parameters of random forests are node size and mtry. We have performed some preliminary analysis in class. The goal of this question is to perform a larger simulation study for these two tuning parameters.
Consider a regression model with five jointly normally distributed predictors. Each predictor has mean 0 and variance 1, and the correlation between any two different predictors is 0.3. The response is
where the noise is independent of the predictors. Thus the true regression function is .
Set up a simulation study with 100 repetitions. Generate 200 test predictor vectors once and keep them fixed. In each repetition, generate 200 new training observations and use the same training data for all tuning settings. Fit 500-tree forests, drawing 200 observations with replacement for each tree. Make the following two comparisons:
- Fix
mtryat 3 and vary node size over . - Fix node size at 10 and vary
mtryover .
Leave all other fitting parameters at their defaults. Record the predictions at each test point across repetitions. For each setting, estimate the squared bias and variance at each test point, then average them over the 200 test points. Compare predictions with when estimating bias. Produce two figures, one for each tuning parameter, showing the averaged squared bias and variance. Set a random seed and report it.
- Fix
Discuss your results in terms of the bias-variance trade-off:
- How does a large node size, such as 30, affect the fit in this example?
- What are the benefits and drawbacks of using a large
mtryvalue? - If each tree instead sampled 100 observations with replacement, would you expect the preferred node size to shift toward smaller or larger values? Explain your reasoning. You may use an additional simulation to help, but a particular pattern is not guaranteed.
Question 2: Tuning random forests for MNIST
Use mnist-first2000.csv.gz from the combined ZIP. It contains 2,000 MNIST observations, with the digit label Digit and 784 pixel values.
- From the first 1,000 observations, keep digits 1, 6, and 7 as the training data. From the remaining 1,000 observations, keep the same digits as the test data.
- Fit PCA to the training pixels, centering but not scaling them. Use the first 20 principal components as predictors, and project the test observations using the same training means and loadings.
Complete the following tasks using RLT classification forests with 1,000 trees. For each tree, draw a bootstrap sample of the same size as the training set.
Fit a random forest with the default
mtryand node-size settings. Report the out-of-bag (OOB) misclassification rate.Construct a grid of
mtryand node-size values to tune the model. Select the setting using OOB misclassification rate. Report the OOB error for each setting and the selected values. Calculate and present the selected forest’s OOB permutation variable importance for the 20 principal components. Comment on your results.Apply the selected model to the test data and report the misclassification rate and confusion matrix. Use the test labels only for this final assessment.
Question 3: Planning a random-forest analysis skill
In earlier homework, some students wrote long skill files with detailed instructions, while others wrote short skills emphasizing their specific requirements or references. These approaches may perform differently depending on the AI and the task. A capable AI may already know standard ways to perform an analysis, so some instructions may be redundant, add little value, or sometimes make the analysis worse. It may be more useful to specify your personal choices for the analysis. An AI with more limited knowledge may benefit from detailed instructions that remind it of the steps to perform.
Write one paragraph describing how you would write a skill that directs an AI to perform a random-forest analysis and produce a report for a future dataset. Discuss how you would balance standard analysis instructions with your personal preferences, considering the AI’s capabilities.
Provide 10 instructions you would consider including in the skill, ordered from the most basic instructions to the more personal preferences. Your list may cover computation, tuning, tables, figures, interpretation, presentation, or other aspects of a random-forest report.
You only need to discuss how the skill should be written. You are not required to write a complete skill file or test it.