-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRRDA.R
53 lines (51 loc) · 1.58 KB
/
RRDA.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#fits a RRDA model
#Input:
#Input: covariates
#Response: response vector
#sub:regularization order
#CV: binary for CV
#cvScale: binary for regularizing during CV
#Output:
#yhat: prediction
#error: training error
#errorMat: error matrix in case of CV
#stderr: errors stddev in case CV
#sub: optimal regularization order
#canonicalMatrix: canonical covariance matrix
#Pi_k: a priori probs
#canonicalMeans: canonical means matrix
RRDA<-function(Input,Response,sub=NULL,CV=FALSE,cvScale=FALSE){
#init parameters:
error=c()
errorMat=c()#selection error matrix
yhat=c()#training prediction
stderr=c()#standard deviations for CV
if(!is.null(sub)){
out=RRDAfit(Input,Response,sub = sub,modelF=FALSE)
}
if(is.null(sub)){
if(!CV){
out0=RRDAfit(Input,Response,modelF=TRUE)
sub=out0$sub
out=RRDAfit(Input,Response,sub=sub,modelF=FALSE)
}
if(CV){
source("D:/RProject/toolkits/ModelAssessment&Selection.R")
p=ncol(Input)
for(i in 1:p){
out0=crossValidate(Input,Response,type="RRDA",complexity = i,scaleInputs = cvScale,scaleType = "Standardize")
errorMat[i]=mean(out$errorVector)
stderr[i]=out$sdVal
}
sub=which.min(error)
out=RRDAfit(Input,Response,sub=sub,modelF=FALSE)
}
}
canonicalMeans=out$canonicalMeans
Pi_k=out$Pi_k
canonicalMatrix=out$canonicalMat
sub=out$sub
yhat=predictRRDA(Input,out)
error=mean(yhat != Response)
return(list(yhat=yhat,error=error,errorMat=errorMat,stderr=stderr,sub=sub,canonicalMatrix=canonicalMatrix,Pi_k=Pi_k,canonicalMeans=canonicalMeans))
}