Skip to content

Commit

Permalink
new struct ModelEquation, best print of fitted regressions
Browse files Browse the repository at this point in the history
  • Loading branch information
marcosdanieldasilva committed Nov 19, 2024
1 parent ebc078a commit 3d2eb9c
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 4 deletions.
17 changes: 13 additions & 4 deletions docs/src/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions src/ForestMensuration.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ include("show.jl")

export
# Regression
ModelEquation,
TableRegressionModel,
regression,
prediction,
Expand Down
2 changes: 2 additions & 0 deletions src/criteria_functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,15 @@ 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
top_models = ct[1:best, 1]
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
Expand Down
5 changes: 5 additions & 0 deletions src/show.jl
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
30 changes: 30 additions & 0 deletions src/structs_consts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3d2eb9c

Please sign in to comment.