## ----setup, include=FALSE-----------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  warning = FALSE,
  message = FALSE,
  fig.width = 5.8,
  fig.height = 4.0,
  fig.align = "center"
)

library(leafareaR)
library(knitr)

data("leafarea_sample", package = "leafareaR")

dat <- la_validate_input(leafarea_sample)

dat2 <- la_create_derived(
  dat,
  variables = c("LW", "L2", "W2", "L3", "W3", "L_plus_W")
)

desc <- la_descriptive_stats(dat2, variables = c("L", "W", "LA", "LW"))
desc_out <- desc[, c("variable", "mean", "sd", "min", "max")]
names(desc_out) <- c("Variable", "Mean", "SD", "Min", "Max")

fit_linear <- la_fit_linear_models(dat2)
met_linear <- la_evaluate_linear_models(fit_linear)
ranked_linear <- la_rank_models(met_linear)
top_linear <- la_top_models(ranked_linear, n = 3)

top_linear_out <- top_linear[, c("model_id", "RMSE", "MAE", "R2", "AIC")]
names(top_linear_out) <- c("Model", "RMSE", "MAE", "R2", "AIC")

best_id <- ranked_linear$model_id[1]
best_model <- fit_linear$models[[best_id]]
best_equation <- la_build_equation(best_model)

pred <- la_predict_top_ranked(
  ranked_table = ranked_linear,
  fit_object = fit_linear,
  rank_position = 1,
  newdata = dat2[1:10, ]
)

pred_out <- pred[1:6, c("LA", "LA_pred", "residual")]
names(pred_out) <- c("Observed LA", "Predicted LA", "Residual")

vals <- la_linear_fitted_values(fit_linear, best_id)

## -----------------------------------------------------------------------------
kable(leafarea_sample[1:6, c("L", "W", "LA")], digits = 3)

## ----echo=TRUE----------------------------------------------------------------
dat <- la_validate_input(leafarea_sample)

dat2 <- la_create_derived(
  dat,
  variables = c("LW", "L2", "W2", "L3", "W3", "L_plus_W")
)

## -----------------------------------------------------------------------------
kable(desc_out, digits = 3)

## ----echo=TRUE----------------------------------------------------------------
fit_linear <- la_fit_linear_models(dat2)
met_linear <- la_evaluate_linear_models(fit_linear)
ranked_linear <- la_rank_models(met_linear)

## -----------------------------------------------------------------------------
kable(top_linear_out, digits = 4)

## ----results='asis'-----------------------------------------------------------
cat(best_equation)

## ----echo=TRUE----------------------------------------------------------------
pred <- la_predict_top_ranked(
  ranked_table = ranked_linear,
  fit_object = fit_linear,
  rank_position = 1,
  newdata = dat2[1:10, ]
)

## -----------------------------------------------------------------------------
kable(pred_out, digits = 4)

## -----------------------------------------------------------------------------
la_plot_scatter(dat2, x = "LW", y = "LA")

## -----------------------------------------------------------------------------
la_plot_observed_predicted(
  observed = vals$observed,
  predicted = vals$fitted,
  model_name = best_id
)

## ----eval=FALSE---------------------------------------------------------------
# fit_nonlinear <- la_fit_nonlinear_models(dat2, models = c("power_LW"))
# met_nonlinear <- la_evaluate_nonlinear_models(fit_nonlinear)
# ranked_nonlinear <- la_rank_models(met_nonlinear)
# 
# fit_mixed <- la_fit_mixed_models(dat2, group_var = "species")
# met_mixed <- la_evaluate_mixed_models(fit_mixed)
# ranked_mixed <- la_rank_models(met_mixed)

## ----eval=FALSE---------------------------------------------------------------
# run_leafareaR_app()

