View source: R/allometric_stats.R
1 |
data |
|
model |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | ##---- Should be DIRECTLY executable !! ----
##-- ==> Define data, use random,
##-- or do help(data=index) for the standard data sets.
## The function is currently defined as
function (data, model)
{
library(randomForest)
library(e1071)
library(caret)
library(ggplot2)
ggplotRegression <- function(fit) {
a <- signif(coef(fit)[1], digits = 5)
b <- signif(coef(fit)[2], digits = 5)
if (coef(fit)[2] >= 0) {
textlab <- paste("y = ", a, " + ", b, "x", sep = "")
}
else {
textlab <- paste("y = ", a, " - ", b, "x", sep = "")
}
options(repr.plot.width = 4, repr.plot.height = 4)
ggplot(fit$model, aes_string(x = names(fit$model)[1],
y = names(fit$model)[2])) + geom_point() + geom_smooth(method = "lm",
col = "red", size = 0.5, se = TRUE) + labs(x = "Observations",
y = "Predictions", title = paste("Adj. R2 = ", signif(summary(fit)$adj.r.squared,
5), " | ", textlab)) + theme(plot.title = element_text(size = 8,
face = "bold"))
}
traindata <- base::sample(nrow(data), size = 0.7 * nrow(data),
replace = FALSE)
TrainSet <- data[traindata, ]
ValidSet <- data[-traindata, ]
ctrl <- caret::trainControl(method = "cv", number = 5, savePredictions = TRUE)
if (model == "mod1") {
mod <- train(log(VUB) ~ I(log(DBH)) + I(log(H)) + I(log(BA)),
data = TrainSet, method = "lm", trControl = ctrl,
metric = "Rsquared")
predictions <- predict(mod, ValidSet)
predicted_V <- data.frame(log(ValidSet$VUB), predictions)
fit.mod <- lm(log(ValidSet$VUB) ~ predictions, data = predicted_V)
g <- ggplotRegression(fit.mod)
print(mod$finalModel)
g
}
else if (model == "mod2") {
mod <- train(log(VUB) ~ I(log(DBH)) + I(log(H)) + I(log(DBH)^2) +
I(log(H)^2), data = TrainSet, method = "lm", trControl = ctrl,
metric = "Rsquared")
predictions <- predict(mod, ValidSet)
predicted_V <- data.frame(log(ValidSet$VUB), predictions)
fit.mod <- lm(log(ValidSet$VUB) ~ predictions, data = predicted_V)
g <- ggplotRegression(fit.mod)
print(mod$finalModel)
g
}
else if (model == "mod3") {
mod <- train(log(VUB) ~ I(log(DBH^2 * H)), data = TrainSet,
method = "lm", trControl = ctrl, metric = "Rsquared")
predictions <- predict(mod, ValidSet)
predicted_V <- data.frame(predictions, log(ValidSet$VUB))
fit.mod <- lm(log(ValidSet$VUB) ~ predictions, data = predicted_V)
g <- ggplotRegression(fit.mod)
print(mod$finalModel)
g
}
else if (model == "mod4") {
set.seed(10)
mod <- train(log(VUB) ~ I(log(DBH^2)) + I(log(H^2)),
data = TrainSet, method = "lm", trControl = ctrl,
metric = "Rsquared")
predictions <- predict(mod, ValidSet)
predicted_V <- data.frame(predictions, log(ValidSet$VUB))
fit.mod <- lm(log(ValidSet$VUB) ~ predictions, data = predicted_V)
g <- ggplotRegression(fit.mod)
print(mod$finalModel)
g
}
else if (model == "mod5") {
set.seed(10)
mod <- train(log(VUB) ~ I(log(H)), data = TrainSet, method = "lm",
trControl = ctrl, metric = "Rsquared")
predictions <- predict(mod, ValidSet)
predicted_V <- data.frame(predictions, log(ValidSet$VUB))
fit.mod <- lm(log(ValidSet$VUB) ~ predictions, data = predicted_V)
g <- ggplotRegression(fit.mod)
print(mod$finalModel)
g
}
else if (model == "modRF") {
set.seed(10)
mod <- train(log(VUB) ~ I(log(H)), data = TrainSet, method = "rf",
ntree = 1000, trControl = ctrl, metric = "Rsquared",
importance = T, proximity = T)
predictions <- predict(mod, ValidSet)
predicted <- data.frame(predictions, log(ValidSet$VUB))
fit.mod <- lm(log(ValidSet$VUB) ~ predictions, data = predicted)
g <- ggplotRegression(fit.mod)
fit.mod$coefficients
print(mod$finalModel)
g
}
else if (model == "modRF2") {
rf <- randomForest(VUB ~ H, TrainSet, ntree = 1000, importance = T,
proximity = TRUE, keep.forest = TRUE)
print(rf)
plot(rf, main = "Random Forest")
predictions <- predict(rf, ValidSet)
predicted <- data.frame(log(predictions), log(ValidSet$VUB))
fit.mod <- lm(log(ValidSet$VUB) ~ log(predictions), data = predicted)
plot(predicted) + abline(fit.mod)
}
else {
stop("No Model selected. Please select model eg., 'mod1','mod2','mod3','mod4','mod5' or 'modRF'")
}
}
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.