| #------------------------------------------------------------- |
| # |
| # 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. |
| # |
| #------------------------------------------------------------- |
| |
| crossV = function(Matrix[double] X, Matrix[double] y, double lamda, Integer k) return (Matrix[double] R) |
| { |
| #create empty lists |
| dataset_X = list(); #empty list |
| dataset_y = list(); |
| fs = ceil(nrow(X)/k); |
| off = fs - 1; |
| #devide X, y into lists of k matrices |
| for (i in seq(1, k)) { |
| dataset_X = append(dataset_X, X[i*fs-off : min(i*fs, nrow(X)),]); |
| dataset_y = append(dataset_y, y[i*fs-off : min(i*fs, nrow(y)),]); |
| } |
| |
| beta_list = list(); |
| #keep one fold for testing in each iteration |
| for (i in seq(1, k)) { |
| [tmpX, testX] = remove(dataset_X, i); |
| [tmpy, testy] = remove(dataset_y, i); |
| trainX = rbind(tmpX); |
| trainy = rbind(tmpy); |
| beta = SimlinRegDS(trainX, trainy, lamda, ncol(X)); |
| beta_list = append(beta_list, beta); |
| } |
| |
| R = cbind(beta_list); |
| } |
| |
| SimlinRegDS = function(Matrix[Double] X, Matrix[Double] y, Double lamda, Integer N) return (Matrix[double] beta) |
| { |
| A = (t(X) %*% X) + diag(matrix(lamda, rows=N, cols=1)); |
| b = t(X) %*% y; |
| beta = solve(A, b); |
| } |
| |
| |
| X = rand(rows=$1, cols=$2); |
| y = X %*% rand(rows=$2, cols=1); |
| |
| R = crossV(X, y, 0.001, 7); |
| |
| r = as.matrix(sum(R!=0)); |
| write(r, $3); |
| #expected: "Result: $2*7 |