Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using our own aic and bic instead of GLM's versions. #13

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/fitmodel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ Type to represent frequentist regression models returned by `fitmodel` functions
"""
struct FrequentistRegression{RegressionType}
model
ndims
end

"""
```julia
FrequentistRegression(::Symbol, model)
FrequentistRegression(::Symbol, model, ndims)
```

Constructor for `FrequentistRegression`. `model` can be any regression model. Used by `fitmodel` functions to return a frequentist regression model containers.
"""
FrequentistRegression(RegressionType::Symbol, model) = FrequentistRegression{RegressionType}(model)
FrequentistRegression(RegressionType::Symbol, model, ndims) = FrequentistRegression{RegressionType}(model, ndims)

"""
Type to represent bayesian regression models returned by `fitmodel` functions. This type is used internally by the package to represent all bayesian regression models.
Expand Down
7 changes: 5 additions & 2 deletions src/frequentist/getter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ function loglikelihood(container::FrequentistRegression)
end

function aic(container::FrequentistRegression)
return StatsBase.aic(container.model)
# container.ndims[2] is the number of parameters
return (2 * container.ndims[2] - 2 * loglikelihood(container))
end

function bic(container::FrequentistRegression)
return StatsBase.bic(container.model)
# container.ndims[1] is the number of data points
# container.ndims[2] is the number of parameters
return (log(container.ndims[1]) * container.ndims[2] - 2 * loglikelihood(container))
end

function sigma(container::FrequentistRegression)
Expand Down
7 changes: 6 additions & 1 deletion src/frequentist/linear_regression.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ julia> plot(cooksdistance(container));
"""
function fitmodel(formula::FormulaTerm, data::DataFrame, modelClass::LinearRegression)
formula = apply_schema(formula, schema(formula, data))
y, X = modelcols(formula, data)
fm_frame = ModelFrame(formula,data)
X = modelmatrix(fm_frame)

model = lm(formula, data)
return FrequentistRegression(:LinearRegression, model)
ndims = (size(X, 1), size(X, 2) + 1)
return FrequentistRegression(:LinearRegression, model, ndims)
end
7 changes: 6 additions & 1 deletion src/frequentist/logistic_regression.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,13 @@ end

function logistic_reg(formula::FormulaTerm, data::DataFrame, Link::GLM.Link)
formula = apply_schema(formula, schema(formula, data))
y, X = modelcols(formula, data)
fm_frame=ModelFrame(formula,data)
X = modelmatrix(fm_frame)

model = glm(formula, data, Binomial(), Link)
return FrequentistRegression(:LogisticRegression, model)
ndims = (size(X, 1), size(X, 2))
return FrequentistRegression(:LogisticRegression, model, ndims)
end

"""
Expand Down
7 changes: 6 additions & 1 deletion src/frequentist/negativebinomial_regression.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@ end

function negativebinomial_reg(formula::FormulaTerm, data::DataFrame, Link::GLM.Link)
formula = apply_schema(formula, schema(formula, data))
y, X = modelcols(formula, data)
fm_frame = ModelFrame(formula,data)
X = modelmatrix(fm_frame)

model = glm(formula, data, NegativeBinomial(), Link)
return FrequentistRegression(:NegativeBinomialRegression, model)
ndims = (size(X, 1), size(X, 2))
return FrequentistRegression(:NegativeBinomialRegression, model, ndims)
end

"""
Expand Down
7 changes: 6 additions & 1 deletion src/frequentist/poisson_regression.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ end

function poisson_reg(formula::FormulaTerm, data::DataFrame, Link::GLM.Link)
formula = apply_schema(formula, schema(formula, data))
y, X = modelcols(formula, data)
fm_frame = ModelFrame(formula,data)
X = modelmatrix(fm_frame)

model = glm(formula, data, Poisson(), Link)
return FrequentistRegression(:PoissonRegression, model)
ndims = (size(X, 1), size(X, 2))
return FrequentistRegression(:PoissonRegression, model, ndims)
end

"""
Expand Down