Saturday, 15 March 2014

r - Apply loop in automated forecast -


I have a data in long format. I'm trying to predict individual variables from the frame. I get trapped in the loop [Application] The part question is how can I change manual forecasts with the applicable application?

  Library (forecast) Library (Data Deal) # Get a series of times www = "http: //staff.elena Aut.ac.nz/Paul-Cowpertwait/ts/cbe.dat # Cbe = read.table (www, header = T) # In this case, there is a data.frame in long format to start with df = data.table (CEB [, 2: 3]) DF [, Year = = 1958: 1 99] dfm = melted (DF, id.ver = "year", variable. Name = "indicator", variable.factor = F) # will give warning because beer = no and others are int Dfm [, site: = "a"] dfm2 = copy (dfm) # to duplicate Somewhere site dfm2 [, site = "b"] dfm = rbind (dfm, dfm2) # function to create a time series; Forecasting f.forecast = function (df, mysite, myindicator, forecast.length = 6, frequency = freq) {# site and pointer X = DF [site == mysite & amp; Pointer == myindicator, # time series start.date = to convert = minutes (x $ year) myts = ts (x $ value, frequency = freq, start = start.date) # forecast myfc = forecast (myts, h = Predictive grid () return (myfc)} # manual solution par (mfrow = c (2, 1)) f1 = f.forecast (dfm, mysite = "a", myindicator = "beer", forecast = thumb = 6, Freq = 12) f2 = f.forecast (dfm, mysite = "a", myindicator = "election", forecast.length = 6, freq = 12) # How to loop? [Many of the sites in the actual data set Variables are] equal (mfrow = c (2,1)) myindicators = unique (Dfm $ indicator) sapply (myindicator, f.forecast (dfm, "a", myindicator = myindicators, prediction = length = 6, frich = 12)) # does not work  

D partition and the second of f.forecast And the third argument is left out. You want to pass directly to the subset of data.frame for example:

  f.forecast = function (x, forecast. Length = 6, frequency = freak) {#comment to first line #x = df [site == mysite & amp; Indicator == myindicator, # is the part of the plot # plot plot (myfc, main = paste (x $ site [1], x dollar indicator [1], sp = "/")}} / code>  

You can now divide the whole df and call f.forecast for each subset:

  dflist & Lt; -split (df, df [, c ("site", "pointer")], drop = true) lapply (dflist, f.forecast)  

No comments:

Post a Comment