0 votes
1 view
in Data Science by (18.3k points)

Can anyone guide me on how to use a for loop for know the minimum RMSE value and also to "apply function" to predict each column. 

The dataset I working on is:

T 6753 6763 6803 6806 6777 6799 6809 6832 6838 6831 6838 6807 6782 6809 6785 6766 6788 6704 6656 7093 7091 7100 7074 7047 7063 7070 7068 7054 7056 7067 7040 7027 7032 7055 7058 7051 7074 7109 7103 7127 7121 7111 7123 7147 7119 7106 7103 7091 7097 7103 7086 7099 7094 7139 7186 7198 7248 7274 7319 7329 7384 7410 7479

C 2307 2296 2297 2287 2273 2259 2246 2230 2215 2194 2175 2110 2098 2074 2070 2107 2117 2128 2106 1687 1674 1664 1638 1641 1672 1679 1677 1675 1681 1675 1665 1697 1694 1693 1693 1691 1703 1706 1700 1695 1698 1712 1688 1701 1693 1674 1690 1688 1710 1711 1692 1688 1700 1684 1755 1744 1764 1762 1753 1753 1768 1763 1788

My target variables are 2 columns, one is Holt and the other one is beta. The problem is I am not knowing how to assign beta to each column by using the function.

The code RMSE is as follows. This for one target variable. 

beta<-seq(.05,.9,by=0.001)

RMSE<-NULL

for (i in seq_along(beta)){

  fit<-holt(cretrain, beta=beta[i],h = length(cretest))

  RMSE[i]<-accuracy(fit,cretest)[2,2]

}

beta.fit<-data_frame(beta,RMSE)

beta.min<-filter(beta.fit,RMSE==min(RMSE))

This is the code which I used to forecast:

list<-apply(df,2,function(x)holt(ts(x,start = c(2015,1),end = c(2020,1),frequency = 12),h=2,beta=.5))

1 Answer

0 votes
by (36.8k points)
edited by

Using lapply we can loop the columns. then you can loop it to 'beta' values. After that, you can apply the holt, rbind to calculate the accuracy for each beta. 

Use the subset function and pass the rows which have minimum RMSE. This returns each column which is in the dataset.

beta <- seq(.05, .9,by=0.001)

out <- lapply(df, function(x) {

         beta.fit <- do.call(rbind, lapply(beta, function(b) {

             fit <- holt(x, beta = b, h = length(x))

             print(head(fit$mean))

               data.frame(beta = b, RMSE =  accuracy(fit, x)[2,2])

             }))

       subset(beta.fit, RMSE == min(RMSE))

     })

 If you want to know more about the Data Science then do check out the following Data Science which will help you in understanding Data Science from scratch

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...