blob: 57759fac1e5de61aefa4e9966f5af7d94f4f85af [file] [log] [blame]
#-------------------------------------------------------------
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#
#-------------------------------------------------------------
m_cvlm = function(Matrix[Double] X, Matrix[Double] y, Integer k, Integer icpt = 0, Double reg = 1e-7) return (Matrix[Double] y_predict, Matrix[Double] allbeta)
{
M = nrow(X);
lim = as.integer(M/k);
y_predict = y;
allbeta = matrix(0, rows=k, cols=ncol(X));
for (i in 1:k)
{
testS = ifelse(i==1, 1, ((i-1) * lim)+1)
testE = i * lim;
testSet = X[testS:testE,];
if (i == 1) {
trainSet = X[testE+1:M,];
trainRes = y[testE+1:M,];
}
else {
trainSet = rbind(X[1:testS-1,], X[testE+1:M,]);
trainRes = rbind(y[1:testS-1,], y[testE+1:M,]);
}
beta = lm(X=trainSet, y=trainRes, icpt=icpt, reg=reg);
pred = lmpredict(X=testSet, w=beta, icpt=icpt);
y_predict[testS:testE,] = pred;
allbeta[i,] = t(beta);
}
}