Regression

Clear previous data

. clear

Setup

. sysuse auto

Randomize the data set

. set seed 1

. gen u = uniform()

. sort u

Fit a random forest regression model

. rforest price weight length, type(reg) iter(500)

Output the statistics computed so far. Notice that the out-of-bag error is computed at this stage.

. ereturn list

Compute expected values of variable price

. predict p1

List the first five entries for predicted prices and actual prices

. list p1 price in 1/5

Output the statistics computed so far. Notice that the mean absolute error and root mean squared error are computed at this stage.

. ereturn list

(click to run)

Classification

Clear previous data

. clear

Setup

. sysuse auto

Randomize the data set

. set seed 1

. gen u = uniform()

. sort u

Fit a random forest classification model

. rforest foreign weight length, type(class) iter(500)

Output the statistics computed so far. Notice that the out-of-bag error is computed at this stage.

. ereturn list

Compute expected classes of variable foreign

. predict p1

Compute expected class probabilities of variable foreign

. predict c1 c2, pr

List the first five entries predicted classes, actual classes, class probabilities for foreign = 0 and for foreign = 1

. list p1 foreign c1 c2 in 1/5

Output the statistics computed so far. Notice that the error rate and fMeasure has been computed at this point.

. ereturn list

(click to run)

Variable Importance

Clear previous data

. clear

Setup

. sysuse auto

Randomize the data set

. set seed 1

. gen u = uniform()

. sort u

Fit a random forest classification model

. rforest weight foreign trunk length mpg price, type(reg)

Output the statistics computed so far. Notice that the out-of-bag error is computed at this stage.

. ereturn list

Compute expected values for variable weight

. predict pred

List the first five entries of variables trunk, pred, foreign and weight

. list trunk pred foreign weight in 1/5

Create a copy of the variable importance matrix stored in e()

. matrix importance = e(importance)

Convert the matrix to a variable

. svmat importance

List the first five entries in the variable importance

. list importance in 1/5

Generate new variable, id, to be used for labeling

. gen id=""

Attach unique labels to individual columns in the chart

local mynames : rownames importance

local k : word count `mynames'

// If there are more variables than observations

if `k'>_N {

set obs `k'

}

forvalues i = 1(1)`k' {

local aword : word `i' of `mynames'

local alabel : variable label `aword'

if ("`alabel'"!="") qui replace id= "`alabel'" in `i'

else qui replace id= "`aword'" in `i'

}

Graph the results

. graph hbar (mean) importance, over(id, sort(1)) ytitle(Importance)

(click to run)