#ARM-Tweedie #################input################ #######parameters #K: number of candidate models #rept: number of data splittings ####### data for training the model averaging weights #Pred_Candidate: n by K matrix of predictions, each row representing a candidate prediction #y: n by 1 vector, the true value of the repsonse library(Tweedie) ARMT=function(K, rept, Pred_Candidate,y){ w=matrix(NA,rept,K) n=nrow(X) n0=floor(n/2) for (j in 1:rept){ index_trainn=sample(n,n0) for(i in 1:K){ tt=(y[index_trainn]-Pred_Candidate[index_trainn,i]) tt1=(y[-index_trainn]-Pred_Candidate[-index_trainn,i]) CC <- sum(tt1^2)/2 CC1<- sum(abs(tt1)) sigmahat=sqrt(sum(tt^2)/n0) sigmahat1=sum(abs(tt))/n0 w[j,i]=sum(log(dtweedie(y[-index_trainn],mu=(abs(Pred_Candidate[-index_trainn,i])+1)^(0.5),phi=var(y[index_trainn])/mean(y[index_trainn])^1.5,power=1.5))) } w[j,]=exp(w[j,]-max(w[j,])) w[j,]=w[j,]/sum(w[j,]) w1=colMeans(w) } #returning the ARM-Tweedie weights return(w1) } #all_predictions_candidate: N by K matrix that contains all the candidate predictions of your data #The ARM-tweedie estimator is as follows. pred_ARM_tweedie=as.matrix(all_predictions_candidate)%*%w1