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

added hypothesis tests #16

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
24 changes: 21 additions & 3 deletions src/estimation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,23 @@ function eval_std_error(sigma::Matrix{T}, num_par::Int) where {T<:Real}
return std_error
end

function z_value(M::Vector{T}, std::Vector{T}) where T
z= M./std
return z
end

function calc_p_value(z::Vector{T}, num_par::Int) where {T<:Real}
pvalue = zeros(num_par)
for (index, value) in enumerate(z)
if value<=0
value=-value
end
pvalue[index] = value
end
return 2*ccdf.(Normal(), pvalue)
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move to the hypothsis_test.jl file



function deviance_residuals(y::Vector{Int}, pi_hat::Vector{T}) where T
return sign.(y.-pi_hat) .* (-2 .* (y .* log.(pi_hat) + (1 .- y) .* log.(1 .- pi_hat))).^(1/2)
end
Expand Down Expand Up @@ -88,9 +105,10 @@ function logreg(y::Vector{Int}, X::Matrix{T}; threshold::T = 0.5) where T
dev_residuals_var = deviance_residuals_variance(dev_residuals)
sigma = eval_sigma(y, X, beta_hat, num_obs, num_par)
std_error = eval_std_error(sigma, num_par)
# zvalue = z_value(beta_hat, std_error)
# z_test_p_value = calc_p_value(zvalue, num_par)
zvalue = z_value(beta_hat, std_error)
z_test_p_value = calc_p_value(zvalue, num_par)

return Model(y, X, threshold, num_obs, beta_hat, pi_hat, y_hat, dof_log, dof_resid,
dof_total, llk, aic, bic, dev_residuals, dev_residuals_var, sigma, std_error)
dof_total, llk, aic, bic, dev_residuals, dev_residuals_var, sigma, std_error,
zvalue, z_test_p_value)
end
4 changes: 2 additions & 2 deletions src/structures.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ mutable struct Model{T <: Real}
dev_residuals_var::Float64
sigma::Matrix{T}
std_error::Vector{T}
# z_value::Vector{T}
# z_test_p_value::Vector{T}
z_value::Vector{T}
z_test_p_value::Vector{T}
end
9 changes: 7 additions & 2 deletions test/simple_models.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
@test model_1.bic ≈ 9.86172 atol = 1e-3
@test model_1.dev_residuals ≈ [-1.5391528, 0.5460551, -0.7675418, 0.7675169, 1.4823421, -0.4852760] atol = 1e-3
@test model_1.std_error ≈ [3.353, 1.026] atol = 1e-3
end
end

@testset "model X_2" begin
model_2 = logreg(y, X_2)
Expand All @@ -58,4 +58,9 @@
@test model_2.y_hat == [1, 1, 0, 1, 1, 0]
@test model_2.std_error ≈ [4.8242, 1.3811, 1.3498] atol = 1e-3
end
end
@testset "hypothesis test" begin
model_2 = logreg(y, X_2)
@test model_2.z_value ≈ [ 0.149, -0.936, 0.895] atol = 1e-3
@test model_2.z_test_p_value ≈ [0.882, 0.349, 0.371] atol = 1e-3
end
end