对于R中的循环与预测

问题描述 投票:0回答:1

我想使用glmnet包在不同的alphas上运行预测。

y是(14x1)因变量,x是自变量的序列(14x19)。

下面的输出数据。

x <- Macro[1:13,3:21]
x <- as.matrix(x)   
y <- Macro[1:13,2:2]
y <- as.matrix(y)
t <- Macro[14:14,3:21]
t <- as.matrix(t)

理想情况下,我希望为每个alpha返回不同的预测:

for (alpha in c(0,.1,.3,.5,.7,.9,1)) {
cv.fit=cv.glmnet(x,y, alpha = alpha, lambda = NULL, nfolds = 5)
min <- cv.fit$lambda.min
fit <- cv.glmnet(x, y, alpha=alpha, lambda = cv.fit$lambda.min, nfolds=5)
predict(fit ,t, s = min)
}

但是这会返回一个错误:

Error in cv.glmnet(x, y, alpha = alpha, lambda = cv.fit$lambda.min, nfolds = 5) : 
Need more than one value of lambda for cv.glmnet

注意代码:

 for (alpha in c(0,.1,.3,.5,.7,.9,1)) {
  fit <- cv.glmnet(x,y, alpha=alpha, nfolds = 5)
}

工作良好。

我该如何修复我的代码?我真的需要在循环中为alphas执行预测函数,而不仅仅是拟合。

dput(x)
structure(c(4.652228652, 4.452166281, 5.553326349, 4.188964308, 
5.012729352, 4.538928371, 5.638139338, 3.925012902, 4.682906379, 
4.660168251, 5.952803094, 4.721206697, 5.758055685, 1.903978779, 
1.837148025, 2.027326755, 1.686266994, 2.017370721, 1.773818155, 
1.937293325, 1.647630534, 1.749950342, 1.730985306, 2.263447785, 
1.98126732, 2.434642854, 2.481570287, 2.33258972, 2.912199168, 
2.110299378, 2.595402032, 2.322950029, 2.842142129, 1.979896372, 
2.319502793, 2.359240313, 2.937856446, 2.399225856, 2.827140745, 
5.082707128, 4.712472353, 5.516298089, 4.168246822, 5.267110329, 
4.833249636, 5.828918236, 4.140403816, 5.28575776, 4.955121909, 
6.404571778, 4.880203713, 6.640952257, 2.320933796, 2.120317491, 
2.593030466, 1.969162713, 2.195998477, 2.198837636, 2.6051228, 
1.87366517, 2.1863434, 2.112724392, 2.625023126, 2.069334825, 
2.328814677, 2.468942531, 2.46345831, 2.99338684, 2.349608577, 
2.568479669, 2.600346713, 3.056925547, 2.129869136, 2.449644735, 
2.352858179, 2.924043472, 2.26104673, 2.411660085, 1.692902733, 
1.541408483, 1.947110878, 1.520008357, 1.671217322, 1.660831673, 
1.992684923, 1.40046815, 1.644093122, 1.581270255, 1.937395811, 
1.519664787, 1.77809053, 2.130588433, 1.928360314, 2.501628649, 
1.930166962, 2.19632807, 2.024905776, 2.667980643, 1.837998822, 
2.346063091, 2.137170693, 2.745812681, 2.111553082, 2.352218846, 
0.040721902, 0.039608433, 0.055438722, 0.037340065, 0.044442281, 
0.041467602, 0.056821845, 0.036648017, 0.044310579, 0.042240444, 
0.060200942, 0.03989574, 0.05027253, 0.019096759, 0.01877026, 
0.024778923, 0.016531124, 0.019846859, 0.017973319, 0.024291258, 
0.017512498, 0.019927366, 0.018047551, 0.027240891, 0.019572431, 
0.024967503, 0.021136035, 0.019737199, 0.029282295, 0.018555507, 
0.022862438, 0.020947225, 0.029544409, 0.018154018, 0.021944567, 
0.021016969, 0.030287821, 0.020090802, 0.024800349, 0.044050512, 
0.041113277, 0.054492472, 0.038055331, 0.047872178, 0.044639163, 
0.059491776, 0.039308651, 0.04955884, 0.047517015, 0.064765233, 
0.043519587, 0.060235515, 0.029467808, 0.027428719, 0.036827997, 
0.02638519, 0.029086009, 0.0290847, 0.037315356, 0.025529541, 
0.029876725, 0.028196018, 0.037560361, 0.026557948, 0.030220349, 
0.03750512, 0.03653175, 0.048462325, 0.036646121, 0.038238758, 
0.038760921, 0.049783835, 0.033023068, 0.037417803, 0.035506876, 
0.046763041, 0.033261873, 0.034905163, 0.022926791, 0.021173154, 
0.029388903, 0.021020651, 0.023448143, 0.023490222, 0.030767307, 
0.020584604, 0.024119676, 0.022463317, 0.029776277, 0.020834843, 
0.024352043, 0.025042587, 0.023646145, 0.032299653, 0.024133764, 
0.026572026, 0.024691929, 0.034760273, 0.023298189, 0.02887728, 
0.026780364, 0.035738399, 0.024864652, 0.027768462, 1, 0, 0, 
0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 
0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0), .Dim = c(13L, 19L
), .Dimnames = list(c("1", "2", "3", "4", "5", "6", "7", "8", 
"9", "10", "11", "12", "13"), c("c1372", "c5244", "c5640", "c6164", 
"b1372", "b5244", "b5640", "b6164", "v1372", "v5244", "v5640", 
"v6164", "bv1372", "bv5244", "bv5640", "bv6164", "s1", "s2", 
"s3")))
> dput(y)
structure(c(867.46, 825.258, 1013.406, 762.577, 890.568, 862.491, 
1030.2, 761.2, 872.93, 892.77, 1089.12, 855.69, 992.454), .Dim = c(13L, 
1L))
> dput(t)
structure(c(5.154502875, 2.04158867, 2.717400485, 6.035902763, 
2.22687594, 2.513986337, 1.77744695, 2.146230648, 0.046828898, 
0.021535376, 0.024242411, 0.056390357, 0.029433671, 0.03603116, 
0.024883907, 0.026647629, 0, 1, 0), .Dim = c(1L, 19L), .Dimnames = list(
    "14", c("c1372", "c5244", "c5640", "c6164", "b1372", "b5244", 
    "b5640", "b6164", "v1372", "v5244", "v5640", "v6164", "bv1372", 
    "bv5244", "bv5640", "bv6164", "s1", "s2", "s3")))
r glmnet
1个回答
0
投票

刚刚放

predict(cv.fit ,t, s = 'lambda.min')

代替

fit <- cv.glmnet(x, y, alpha=alpha, lambda = cv.fit$lambda.min, nfolds=5)
predict(fit ,t, s = min)

无需再次安装模型

© www.soinside.com 2019 - 2024. All rights reserved.