From 3d2eb9c788f3063b5ac5a7de29ff506abb8135db Mon Sep 17 00:00:00 2001 From: marcos Date: Tue, 19 Nov 2024 12:28:19 -0300 Subject: [PATCH] new struct ModelEquation, best print of fitted regressions --- docs/src/tutorial.md | 17 +++++++++++++---- src/ForestMensuration.jl | 1 + src/criteria_functions.jl | 2 ++ src/show.jl | 5 +++++ src/structs_consts.jl | 30 ++++++++++++++++++++++++++++++ 5 files changed, 51 insertions(+), 4 deletions(-) diff --git a/docs/src/tutorial.md b/docs/src/tutorial.md index e5ae2d0..34a022e 100644 --- a/docs/src/tutorial.md +++ b/docs/src/tutorial.md @@ -74,17 +74,26 @@ data = DataFrame( # Performing the regression analysis on the full dataset # Here, we analyze the relationship between height (h) and diameter (dbh) -models = regression(:h, :dbh, data) +models = regression(:h, :dbh, data); +# Alternative print of fitted models +models_eq = ModelEquation.(models) +``` +```@example regression_data +#number of fitted regressions +length(models) ``` ### Viewing the top models based on specific criteria ```@example regression_data -best_models = criteria_table(models, :adjr2, :rmse) +# Using all criteria and presenting the 10 best models +best_models = criteria_table(models) +``` -# Chossing as especific criteria -best_models = criteria_table(models, :adjr2, :rmse) +```@example regression_data +# Chossing as especific criteria and best 5, if best = false or 0 show all regressions from 'models' +best_models_v2 = criteria_table(models, :adjr2, :rmse, best=5) ``` ### Regression Selection Criteria [`criteria_table`](@ref) diff --git a/src/ForestMensuration.jl b/src/ForestMensuration.jl index c7f62be..023effd 100644 --- a/src/ForestMensuration.jl +++ b/src/ForestMensuration.jl @@ -53,6 +53,7 @@ include("show.jl") export # Regression + ModelEquation, TableRegressionModel, regression, prediction, diff --git a/src/criteria_functions.jl b/src/criteria_functions.jl index 79b17f7..ed80e7d 100644 --- a/src/criteria_functions.jl +++ b/src/criteria_functions.jl @@ -190,6 +190,7 @@ function criteria_table( # If 'best' is false, return the full DataFrame if best === false + ct.model = ModelEquation.(ct.model) return ct elseif best < length(model) # If 'best' is less than the number of models, return the top 'best' models @@ -197,6 +198,7 @@ function criteria_table( return criteria_table(top_models, criteria...; best=false) # Re-run with selected models else # Otherwise, return the full DataFrame + ct.model = ModelEquation.(ct.model) return ct end end diff --git a/src/show.jl b/src/show.jl index 8a4288f..1014c4b 100644 --- a/src/show.jl +++ b/src/show.jl @@ -1,5 +1,10 @@ import Base: show +# Define the show method for custom display of ModelEquation +function show(io::IO, model::ModelEquation) + print(io, model.output) +end + # Custom show method for SiteAnalysis to display the site_table and site_plot function show(io::IO, analysis::SiteAnalysis) show(io, analysis.site_table) diff --git a/src/structs_consts.jl b/src/structs_consts.jl index c0743aa..20b2c26 100644 --- a/src/structs_consts.jl +++ b/src/structs_consts.jl @@ -87,6 +87,36 @@ Newton Method: """ abstract type Newton <: CubingMethod end + +""" + struct ModelEquation + +Define ModelEquation struct to store the regression results + # Fields + - output::String + - model::TableRegressionModel +""" +struct ModelEquation + output::String + model::TableRegressionModel + # Inner constructor to initialize `output` based on `model` + function ModelEquation(model::TableRegressionModel) + # Get coefficients and terms from the model + β = coef(model) + n = length(β) + output = string(StatsModels.coefnames(model.mf.f.lhs)) * " = $(round(β[1], digits = 6))" + + for i in 2:n + term = coefnames(model)[i] + product = string(round(abs(β[i]), sigdigits=6)) * " * " * term + output *= signbit(β[i]) ? " - $(product)" : " + $(product)" + end + + # Return the constructed RegressionEquation object + new(output, model) + end +end + """ struct SiteAnalysis