I have a dataset of 15 variables (1 under examination, and 14 its regressors) all numeric. What I do is that i run an algorithm that is a recursive forecasting technique. This algorithm cuts the data in an in-sample and an out-sample. Here I want to figure out how to store the results produced for each value of a and t, which are parameters of the cv.hqreg function (hqreg package).
- Note: That for each value of
t and awe get 1 value (the one in the code aspredicedQ. For each of thoset and awe run thecv.hqreg648 times. And then again 648 times for the next value oft and a. Thus the ending result will be a matrix/dataset of 648 rows and 231 columns.
For each cv.hqreg I get 100 fitted models from which I select the one with the smallest error via this LMQ$fit$beta[,which(LMQ$lambda.min==LMQ$lambda)] command line.
dataR<-TRAINSET
fittedvaluesQRidge<-NULL
for(i in 1:(nrow(TESTSET)-1)){ #adding a new row and repeat
for(a in seq(0,1,0.1)){ #for each penalty of selection
for(t in seq(0,1,0.05)){ #for each quantile
print(i) #to see it works/or where stops
dataR<-rbind(dataR,TESTSET[i,]) #update dataset
LMQ<-cv.hqreg(as.matrix(dataR[,-15]),dataR$LHS,method = "quantile",tau=t,alpha = a) #FIT THE Lasso Quantile-MODEL
predictdQR<-LMQ$fit$beta[1,which(LMQ$lambda.min==LMQ$lambda)]+LMQ$fit$beta[2,which(LMQ$lambda.min==LMQ$lambda)]*TESTSET[i+1,1]+LMQ$fit$beta[3,which(LMQ$lambda.min==LMQ$lambda)]*TESTSET[i+1,2]+LMQ$fit$beta[4,which(LMQ$lambda.min==LMQ$lambda)]*TESTSET[i+1,3]+LMQ$fit$beta[5,which(LMQ$lambda.min==LMQ$lambda)]*TESTSET[i+1,4]+LMQ$fit$beta[6,which(LMQ$lambda.min==LMQ$lambda)]*TESTSET[i+1,5]+LMQ$fit$beta[7,which(LMQ$lambda.min==LMQ$lambda)]*TESTSET[i+1,6]+LMQ$fit$beta[8,which(LMQ$lambda.min==LMQ$lambda)]*TESTSET[i+1,7]+LMQ$fit$beta[9,which(LMQ$lambda.min==LMQ$lambda)]*TESTSET[i+1,8]+LMQ$fit$beta[10,which(LMQ$lambda.min==LMQ$lambda)]*TESTSET[i+1,9]+LMQ$fit$beta[11,which(LMQ$lambda.min==LMQ$lambda)]*TESTSET[i+1,10]+LMQ$fit$beta[12,which(LMQ$lambda.min==LMQ$lambda)]*TESTSET[i+1,11]+LMQ$fit$beta[13,which(LMQ$lambda.min==LMQ$lambda)]*TESTSET[i+1,12]+LMQ$fit$beta[14,which(LMQ$lambda.min==LMQ$lambda)]*TESTSET[i+1,13]+LMQ$fit$beta[15,which(LMQ$lambda.min==LMQ$lambda)]*TESTSET[i+1,14] #find the forecasts
fittedvaluesQRidge<-c(fittedvaluesQRidge,predictdQR) #then put them in a vector
}
}
}
The commands I have used to get the predicted value are quite extensive using each one variable at a time. However I have tried to use matrix algebra (matrix of the covariates %*% data with no results but an error: non-numeric argument to binary operator. It works, in an ugly yes way, but if there is a shorter way I would like all the assistance.