Added CV function for old reg coeffs
This commit is contained in:
parent
542415dde8
commit
6008056412
2 changed files with 28 additions and 0 deletions
|
|
@ -31,6 +31,7 @@ export TRSegCVNaive
|
|||
export TRSegCVFair
|
||||
|
||||
# From "variousRegressionFunctions.jl"
|
||||
export oldRegCoeffs
|
||||
export PCR
|
||||
export bidiag2
|
||||
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue