Added CV function for old reg coeffs

This commit is contained in:
Joakim Skogholt 2024-05-18 15:07:39 +02:00
parent 542415dde8
commit 6008056412
2 changed files with 28 additions and 0 deletions

View file

@ -31,6 +31,7 @@ export TRSegCVNaive
export TRSegCVFair
# From "variousRegressionFunctions.jl"
export oldRegCoeffs
export PCR
export bidiag2

View file

@ -2,7 +2,34 @@
"""
function oldRegCoeffs(X, y, cvfolds, ncomps, regFunction=bidiag2)
Calculates "old" regression coefficients using CV and the 1 S.E. rule.
"""
function oldRegCoeffs(X, y, cvfolds, ncomps, regFunction=bidiag2) # bidiag2 OR PCR
cverror = zeros(20,1);
for i=1:length(unique(cvfolds))
Xtrain = X[vec(i .!= cvfolds), :];
ytrain = y[vec(i .!= cvfolds), :];
Xtest = X[vec(i .== cvfolds), :];
ytest = y[vec(i .== cvfolds), :];
betas, _ = regFunction(Xtrain, ytrain, ncomps)
cverror += sum((ytest .- Xtest*betas[2:end,:] .- betas[1,:]').^2, dims=1)';
end
cverror = cverror ./ n
#rmsecv = sqrt.(rmsecv ./ size(X, 1))
#minInd = argmin(rmsecv)[1]
regCoeffsInds = findfirst(cverror .< (minimum(cverror) + std(cverror)/sqrt(length(unique(cvfolds)))))[1] # 1 S.E. rule
betas, _ = regFunction(X, y, ncomps)
bold = betas[2:end, regCoeffsInds]
return bold, regCoeffsInds, cverror
end
"""
function PCR(X, y, kmax, centre=true)#, standardize=true)