szcf-weiya / ESL-CN

The Elements of Statistical Learning (ESL)的中文翻译、代码实现及其习题解答。
https://esl.hohoweiya.xyz
GNU General Public License v3.0
2.43k stars 594 forks source link

Ex. 9.6 #239

Closed szcf-weiya closed 3 years ago

szcf-weiya commented 3 years ago

image

szcf-weiya commented 3 years ago

LOESS

Figure 6.9 has been reproduced in https://esl.hohoweiya.xyz/rmds/lattice.html. It shows that

szcf-weiya commented 3 years ago

GAM

# GAM
library(gam)
fit = gam(I(ozone^(1/3))~s(temperature) + s(wind) + s(radiation), data = data)
par(mfrow=c(1, 3))
plot(fit, se = T)

image It is clear that the contribution of each feature captures the same pattern as concluded for Fig. 6.9.

szcf-weiya commented 3 years ago

Tree

library(rpart)
library(rpart.plot)
fit.tree = rpart(I(ozone^(1/3))~ temperature + wind + radiation, data = data)
rpart.plot(fit.tree)

image Note that the response value in the leaf node increases from left to right, then we can observe that

szcf-weiya commented 3 years ago

MARS

> # MARS
> library(earth)
> fit.mars = earth(I(ozone^(1/3))~ temperature + wind + radiation, data = data)
> summary(fit.mars)
Call: earth(formula=I(ozone^(1/3))~temperature+wind+radiation, data=data)

                  coefficients
(Intercept)         2.72035983
h(temperature-73)   0.07721326
h(temperature-90)  -0.11780995
h(9.7-wind)         0.17106469
h(225-radiation)   -0.00317888

Selected 5 of 14 terms, and 3 of 3 predictors
Termination condition: Reached nk 21
Importance: temperature, wind, radiation
Number of terms at each degree of interaction: 1 4 (additive model)
GCV 0.2185924    RSS 20.48861    GRSq 0.7267643    RSq 0.7650625

Note the basis function and the sign of coefficients,

Interestingly, the reflection pairs do not return as in https://github.com/szcf-weiya/ESL-CN/blob/3aefb0bc171e272ca87af159e1d665d1d56b4c9f/code/MARS/simulation.R

szcf-weiya commented 3 years ago

PRIM

library(prim)
fit.prim = prim.box(as.matrix(data[,-1]), as.numeric(data[, 1]))
summary(fit.prim, print.box = T)
plotprim = function(x, y, box = box, ...) {
  plot(x, y, ...)
  rect(box[1, 1], box[1, 2], box[2, 1], box[2, 2], border = "red")
}
colname = colnames(data)
par(mfrow = c(1, 3))
for (i in 2:3)
  for (j in (i+1):4)
    plotprim(data[,i], data[, j], fit.prim$box[[1]][,c(i-1, j-1)], 
             xlab = colname[i], ylab = colname[j])

It seems that plot.prim does not plot the box, and also cannot superimpose on the original observation points, so I wrote the plot function by myself. image Note that the box indicates large ozone, so it reveals that

szcf-weiya commented 3 years ago

Conclusion: all these methods obtain similar results in terms of the trend of every single variable.