Packages
Prepare data and fit the model.
data(mtcars)
xs <- xgb.DMatrix(data = mtcars %>% select(cyl:carb) %>% as.matrix,
label = mtcars$mpg)
f <- xgb.train(params = list(booster = 'gblinear', objective = 'reg:linear')
, data = xs
, nrounds = 100)
R-squared, variance explained.
d <- tibble(pred = predict(f, newdata = xs)
, obs = mtcars$mpg) %>%
mutate(resid = pred - obs,
resid_sq = resid^2)
sstot <- sum((d$pred - mean(d$obs))^2)
ssresid <- sum(d$resid_sq)
sprintf("percent variance explained, R^2: %1.1f%%", 100 * (1 - ssresid / sstot))
## [1] "percent variance explained, R^2: 82.0%"
Density plot of residuals.
ggplot(d, aes(x = resid)) +
geom_density(fill = 'grey50', color = 'white', alpha = 0.7) +
theme_bw()
Observed and predicted.
ggplot(d, aes(x = obs, y = pred, size = resid)) +
geom_point() +
theme_bw()