test_regression.jl (1317B)
1 # test/test_regression.jl 2 using Test, CSV, DataFrames, Statistics 3 4 # R reference coefficients from readme.md (full sample, 264 obs) 5 ref_coefs = Dict( 6 :dp => 3.370, 7 :cay => 1.814, 8 :rf => -1.246, 9 :constant => 0.011, 10 ) 11 ref_r2 = 0.344 12 ref_nobs = 264 13 14 include(joinpath(@__DIR__, "..", "src", "RiskPremium.jl")) 15 using .RiskPremium 16 17 # Use the R reference predict.csv directly (already validated in test_dataimport.jl) 18 predict = CSV.read("output/predict.csv", DataFrame) 19 result = RiskPremium.run_regression(predict) 20 21 println("Coefficients:") 22 println(" dp: $(round(result.coefs[:dp], digits=3)) (ref: $(ref_coefs[:dp]))") 23 println(" cay: $(round(result.coefs[:cay], digits=3)) (ref: $(ref_coefs[:cay]))") 24 println(" rf: $(round(result.coefs[:rf], digits=3)) (ref: $(ref_coefs[:rf]))") 25 println(" const: $(round(result.coefs[:constant], digits=3)) (ref: $(ref_coefs[:constant]))") 26 println("R²: $(round(result.r2, digits=3)) (ref: $ref_r2)") 27 println("N: $(result.nobs) (ref: $ref_nobs)") 28 29 @testset "Regression validation" begin 30 for (k, v) in ref_coefs 31 err = abs(result.coefs[k] - v) 32 println(" $k error: $err") 33 @test err < 0.01 34 end 35 @test abs(result.r2 - ref_r2) < 0.005 36 @test result.nobs == ref_nobs 37 end 38 39 println("✓ Regression coefficients match R output")