如何避免6次写同一个R子句

时间:2016-11-21 13:23:04

标签: r

我根据以下建议的解决方案修改了编辑内容:

library(caret)
library(ggplot2)
library('scales')


Macro <- read.csv("P:/R/Macro.csv")

foo <- function (start) {

  mycontrol <- trainControl(method = "timeslice",
                            initialWindow = 8,
                            horizon = 1,
                            fixedWindow = FALSE, 
                            savePredictions = TRUE)


  myfit <- train(Y ~ X1372C + S1 + S2 + S3,
                 data = Macro[start:14, ,drop = FALSE],
                 method = "lm",
                 trControl = mycontrol)

  c(myfit$pred)  ## return; drop dimension as a vector
}


Forecast <- sapply(1:6, foo)  ## will simplify to matrix

dput(Forecast)

这转化为此 输出:

警告讯息: 1:在nominalTrainWorkflow中(x = x,y = y,wts = weights,info = trainInfo,:   重新采样的绩效指标中存在缺失值。 2:在nominalTrainWorkflow中(x = x,y = y,wts = weights,info = trainInfo,:   重新采样的绩效指标中存在缺失值。 3:在nominalTrainWorkflow中(x = x,y = y,wts = weights,info = trainInfo,:   重新采样的绩效指标中存在缺失值。 4:在nominalTrainWorkflow(x = x,y = y,wts = weights,info = trainInfo,:   重新采样的绩效指标中存在缺失值。 5:在nominalTrainWorkflow中(x = x,y = y,wts = weights,info = trainInfo,:   重新采样的绩效指标中存在缺失值。 6:在nominalTrainWorkflow(x = x,y = y,wts = weights,info = trainInfo,:   重新抽样的绩效指标中缺少值。

  

dput(预测)   结构(c(921.172565647808,926.099130620101,1240.09797589312,   942.482635914736,955.942081354932,977.408884588125),c(977.0702941,   943.8941538,1272.797631,981.859451,995.574098,1052.287953   ),9:14,c(TRUE,TRUE,TRUE,TRUE,TRUE,TRUE),c(&#34; Training1&#34;,   &#34;培训2&#34;,&#34;培训3&#34;,&#34;培训4&#34;,&#34;培训5&#34;,&#34;培训6&#34;   ),c(926.678510620924,1241.54853541131,943.689699182586,952.297326382802,   979.924409311149),c(943.8941538,1272.797631,981.859451,995.574098,   1052.287953),9:13,c(TRUE,TRUE,TRUE,TRUE,TRUE),c(&#34; Training1&#34;,   &#34;培训2&#34;,&#34;培训3&#34;,&#34;培训4&#34;,&#34;培训5&#34;),c(1257.33305898499,   951.366992832791,957.163715037822,986.364307731912),c(1272.797631,   981.859451,995.574098,1052.287953),9:12,c(TRUE,TRUE,TRUE,   TRUE),c(&#34; Training1&#34;,&#34; Training2&#34;,&#34; Training3&#34;,&#34; Training4&#34;),       c(993.427314382052,978.520174690649,1067.16914028792),       c(981.859451,995.574098,1052.287953),9:11,c(TRUE,TRUE,       TRUE),c(&#34; Training1&#34;,&#34; Training2&#34;,&#34; Training3&#34;),c(987.694506364155,       1102.00413769988),c(995.574098,1052.287953),9:10,c(TRUE,       TRUE),c(&#34; Training1&#34;,&#34; Training2&#34;),1058.30501548728,1052.287953,       9L,TRUE,&#34; Training1&#34;),. Dim = 5:6,.Dimnames = list(c(&#34; pred&#34;,   &#34; obs&#34;,&#34; rowIndex&#34;,&#34; intercept&#34;,&#34; Resample&#34;),NULL))

dput(Macro [1:14,]) 结构(列表(Qtrs =结构(1:14,.Label = c(&#34; 14_Q2&#34;,&#34; 14_Q3&#34;, &#34; 14_Q4&#34;,&#34; 15_Q1&#34;,&#34; 15_Q2&#34;,&#34; 15_Q3&#34;,&#34; 15_Q4&#34;,&#34; 16_Q1& #34;,&#34; 16_Q2&#34;, &#34; 16_Q3&#34;,&#34; 16_Q4&#34;,&#34; 17_Q1&#34;,&#34; 17_Q2&#34;,&#34; 17_Q3&#34;),class =&# 34;因子&#34),     Y = c(865.8283846,856.0688462,1165.958691,883.3432512,     902.1949231,897.0950769,1237.512923,962.5309231,977.0702941,     943.8941538,1272.797631,981.859451,995.574098,1052.287953     ),X1372C = c(0.906148417,0.882519787,1.329575232,1.088817819,     1.180987191,1.167632493,1.593644333,1.178410516,1.2626391569,     1.221516083,1.605735851,1.202507143,1.243837663,1.359676971     ),X5244C = c(0.906148417,0.882519787,1.329575232,1.088817819,     1.180987191,1.167632493,1.593644333,1.178410516,1.2626391569,     1.221516083,1.605735851,1.202507143,1.243837663,1.359676971     ),X5640C = c(0.668605303,0.650010329,0.871325692,0.657860513,     0.717979329,0.734934807,0.987398267,0.729793323,0.811565803,     0.820667752,1.072773575,0.783936578,0.864640587,0.955630807     ),X6164C = c(0.405020101,0.372197584,0.518686161,0.373651444,     0.407590328,0.406092873,0.560442245,0.417412494,0.429938649,     0.461566266,0.592980109,0.444907445,0.47447048,0.558013448     ),X1372B = c(1.874539814,1.816307314,2.538860638,1.785564496,     1.877335003,1.896950271,2.677640702,2.057301773,2.060862256,     2.010340805,2.749691737,2.112415534,2.123956718,2.264930431     ),X5244B = c(1.874539814,1.816307314,2.538860638,1.785564496,     1.877335003,1.896950271,2.677640702,2.057301773,2.060862256,     2.010340805,2.749691737,2.112415534,2.123956718,2.264930431     ),X5640B = c(2.784147985,2.728887944,3.915195977,3.036215525,     3.037519069,3.035750269,4.235355932,3.304887671,3.26268204,     3.141199768,4.294197969,3.274759241,3.2111118188,3.501393693     ),X6164B = c(0.791458844,0.78168066,1.18611112,0.859852789,     0.944191582,0.976372468,1.397898629,1.099556909,1.128398283,     1.131976904,1.580815657,1.206596743,1.245306897,1.358211802     ),X1372V = c(0.097143401,0.097524773,0.141261151,0.115168338,     0.120877067,0.119050862,0.157452868,0.117294253,0.11874258,     0.117416967,0.154157986,0.117553225,0.117734259,0.126540804     ),X5244V = c(0.097143401,0.097524773,0.141261151,0.115168338,     0.120877067,0.119050862,0.157452868,0.117294253,0.11874258,     0.117416967,0.154157986,0.117553225,0.117734259,0.126540804     ),X5640V = c(0.064650539,0.063541823,0.083794768,0.063561861,     0.06730385,0.068859437,0.089436159,0.066723151,0.072244789,     0.072641489,0.095058099,0.070520087,0.075122879,0.082610429     ),X6164V = c(0.044563645,0.042226354,0.057467132,0.042275768,     0.043796282,0.043972939,0.059422138,0.044436698,0.044714835,     0.047485033,0.060888955,0.046498904,0.047787208,0.055743044     ),X1372BV = c(0.211443705,0.20868086,0.281834069,0.198443943,     0.205496416,0.207598984,0.285816341,0.21745711,0.21640918,     0.210584615,0.283752378,0.218344658,0.215849274,0.229469246     ),X5244BV = c(0.211443705,0.20868086,0.281834069,0.198443943,     0.205496416,0.207598984,0.285816341,0.21745711,0.21640918,     0.210584615,0.283752378,0.218344658,0.215849274,0.229469246     ),X5640BV = c(0.27897666,0.2771921,0.387651075,0.297277243,     0.293774368,0.293731964,0.400189046,0.309076866,0.302470138,     0.29076674,0.39564161,0.301178985,0.297513626,0.315261748     ),X6164BV = c(0.087276144,0.089506522,0.130605096,0.094950656,     0.101815228,0.105435663,0.148800072,0.115872327,0.118331193,     0.118122767,0.163058,0.124425055,0.126056291,0.136434954     ),S1 = c(1L,0L,0L,0L,1L,0L,0L,0L,1L,0L,0L,0L,     1L,0L),S2 = c(0L,1L,0L,0L,0L,1L,0L,0L,0L,1L,0L,     0L,0L,1L),S3 = c(0L,0L,1L,0L,0L,0L,1L,0L,0L,0L,     1L,0L,0L,0L)),. Name = c(&#34; Qtrs&#34;,&#34; Y&#34;,&#34; X1372C&#34;,&#34; X5244C&#34; , &#34; X5640C&#34;,&#34; X6164C&#34;,&#34; X1372B&#34;,&#34; X5244B&#34;,&#34; X5640B&#34;,&#34; X6164B& #34;,&#34; X1372V&#34;, &#34; X5244V&#34;,&#34; X5640V&#34;,&#34; X6164V&#34;,&#34; X1372BV&#34;,&#34; X5244BV&#34;,&#34; X5640BV& #34 ;, &#34; X6164BV&#34;,&#34; S1&#34;,&#34; S2&#34;,&#34; S3&#34;),row.names = c(NA,14L),class = &#34; data.frame&#34)

2 个答案:

答案 0 :(得分:2)

您的条款仅在使用的data中有所不同。更准确地说,是数据集Macro的子集。更准确地说,是子集的起始行。那么为什么不写一个函数呢?

foo <- function (start) {

  mycontrol <- trainControl(method = "timeslice",
                            initialWindow = 8,
                            horizon = 1,
                            fixedWindow = FALSE, 
                            savePredictions = TRUE)


  myfit <- train(Y ~ X1372C + S1 + S2 + S3,
                 data = Macro[start:14, ,drop = FALSE],
                 method = "lm",
                 trControl = mycontrol)

  c(myfit$pred)  ## return; drop dimension as a vector
  }

然后

Forecast <- sapply(1:6, foo)  ## will simplify to matrix

最后,您的F9F14只是矩阵Forecast的下三角(加上对角线)的行和:

upper.tri(Forecast) <- 0
rowSums(Forecast)

答案 1 :(得分:0)

我很快就完成了你的代码,但这应该可行。我建议您查看defining functions in R

clauseLoop <- function(startPoint) {
    myControl <- trainControl(method = "timeslice",
                      initialWindow = 8,
                      horizon = 1,
                      fixedWindow = FALSE, 
                      savePredictions = TRUE)

    myFit <- train(Y ~ X1372C + S1 + S2 + S3,
                   data = startPoint,
                   method = "lm",
                   trControl = mycontrol)

    Forecast <- myFit$pred
}

ForecastX <- clauseLoop("x <- Macro[6:14,]")