From 7f4c8ce562c22560358b624c077acacad6565fdc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Gutierrez?= Date: Mon, 6 Apr 2020 16:41:09 -0300 Subject: [PATCH 1/3] added hypothesis tests --- src/estimation.jl | 24 +++++++++++++++++++++--- src/structures.jl | 4 ++-- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/estimation.jl b/src/estimation.jl index 29c5578..70068bf 100644 --- a/src/estimation.jl +++ b/src/estimation.jl @@ -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 + + 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 @@ -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) 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 \ No newline at end of file diff --git a/src/structures.jl b/src/structures.jl index fea3ce6..56a140d 100644 --- a/src/structures.jl +++ b/src/structures.jl @@ -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 \ No newline at end of file From f1e19bdbef5f9181e89bfb97a436cbe44d4ac184 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Gutierrez?= Date: Mon, 6 Apr 2020 17:18:36 -0300 Subject: [PATCH 2/3] adding the last versiom --- src/estimation.jl | 2 +- test/simple_models.jl | 9 +++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/estimation.jl b/src/estimation.jl index 70068bf..365b8e1 100644 --- a/src/estimation.jl +++ b/src/estimation.jl @@ -106,7 +106,7 @@ function logreg(y::Vector{Int}, X::Matrix{T}; threshold::T = 0.5) where T 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) + 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, diff --git a/test/simple_models.jl b/test/simple_models.jl index 9225d08..e21ac1c 100644 --- a/test/simple_models.jl +++ b/test/simple_models.jl @@ -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) @@ -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 \ No newline at end of file + @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 From fb10a425ca8f949263b4b449bde3ad40a30f9f87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Gutierrez?= Date: Tue, 7 Apr 2020 13:26:56 -0300 Subject: [PATCH 3/3] moving src/hypothesis_tests.jl --- src/estimation.jl | 17 ----------------- src/hypothesis_tests.jl | 15 +++++++++++++++ 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/src/estimation.jl b/src/estimation.jl index 365b8e1..6b6b530 100644 --- a/src/estimation.jl +++ b/src/estimation.jl @@ -49,23 +49,6 @@ 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 - - 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 diff --git a/src/hypothesis_tests.jl b/src/hypothesis_tests.jl index e69de29..b53e5b1 100644 --- a/src/hypothesis_tests.jl +++ b/src/hypothesis_tests.jl @@ -0,0 +1,15 @@ +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 \ No newline at end of file