BA: Basal area prediction

Usage Arguments Examples

Usage

1

Arguments

data

Examples

 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
##---- 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)
{
    library(randomForest)
    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"))
    }
    res <- cor(data)
    traindata <- base::sample(nrow(data), size = 0.6 * nrow(data),
        replace = FALSE)
    TrainSet <- data[traindata, ]
    ValidSet <- data[-traindata, ]
    ctrl <- caret::trainControl(method = "cv", number = 10, savePredictions = TRUE)
    mod <- train(log(BA) ~ I(log(H)), data = TrainSet, method = "lm",
        trControl = ctrl, metric = "Rsquared")
    predictions <- predict(mod, ValidSet)
    predicted_BA <- data.frame(log(ValidSet$BA), predictions)
    fit.mod <- lm(log(ValidSet$BA) ~ predictions, data = predicted_BA)
    g <- ggplotRegression(fit.mod)
    print(round(res, 3))
    print(mod$finalModel)
    g
  }

wslerry/tRee documentation built on Oct. 5, 2019, 10:30 p.m.