[R] Confused: Looping in dataframes
David Winsemius
dwinsemius at comcast.net
Fri Jun 25 14:28:45 CEST 2010
On Jun 25, 2010, at 7:09 AM, phani kishan wrote:
> On Fri, Jun 25, 2010 at 1:54 PM, Paul Hiemstra
> <p.hiemstra at geo.uu.nl> wrote:
>
>> On 06/25/2010 10:02 AM, phani kishan wrote:
>>
>>> Hey,
>>> I have a data frame x which consists of say 10 vectors. I
>>> essentially want
>>> to find out the best fit exponential smoothing for each of the
>>> vectors.
>>>
>>> The problem while I'm getting results when i say
>>>
>>>
>>>> lapply(x,ets)
>>>>
>>>>
>>> I am getting an error when I say
>>>
>>>
>>>> myprint
>>>>>
>>>>>
>>>> function(x)
>>> {
>>> for(i in 1:length(x))
>>> {
>>> ets(x[i],model="AZZ",opt.crit=c("amse"))
>>>
>>>
>> Hi,
>>
>> Please provide a reproducible example, as stated in the posting
>> guide. My
>> guess is that replacing x[i] by x[[i]] would solve the problem.
>> Double
>> brackets return a vector in stead of a data.frame that has just
>> column i.
>>
> Hey Paul,
> As requested.
> My example data frame
>
> sdata:
> SKU1 SKU2 SKU3 SKU4
> 1 583.8 574.6 1106.9
> 648.1
> 2 441.7 552.8 1021.3
> 353.6
> 3 454.2 555.7 998.3
> 306.4
> 4 569.7 507.6 811.1
> 360.7
> 5 512.3 620.0 1046.3
> 713.9
> 6 580.8 668.2 732.0
> 490.9
> 7 648.5 766.9 653.4
> 422.1
> 8 617.4 657.1 602.1
> 190.8
> 9 826.8 767.3 640.5
> 324.1
> 10 1163.0 657.6 429.6
> 181.1
> 11 643.5 788.9 569.1
> 331.9
> 12 846.9 568.6 425.1
> 224.6
> 13 580.7 582.9 434.2
> 226.9
>
> now when I apply
> lapply(sdata,ets)
> I get a result as:
> $SKU1
> ETS(A,N,N)
>
> Call:
> ets(y = x, model = "AZZ")
>
> Smoothing parameters:
> alpha = 0.3845
>
> Initial states:
> l = 533.3698
>
> sigma: 181.7615
>
> AIC AICc BIC
> 172.6144 173.8144 173.7443
>
> $SKU2
> ETS(A,N,N)
>
> Call:
> ets(y = x, model = "AZZ")
>
> Smoothing parameters:
> alpha = 0.5026
>
> Initial states:
> l = 567.821
>
> sigma: 86.7074
>
> AIC AICc BIC
> 153.3704 154.5704 154.5003
>
> $SKU3
> ETS(A,A,N)
>
> Call:
> ets(y = x, model = "AZZ")
>
> Smoothing parameters:
> alpha = 1e-04
> beta = 1e-04
>
> Initial states:
> l = 1189.2221
> b = -64.3776
>
> sigma: 85.4153
>
> AIC AICc BIC
> 156.9800 161.9800 159.2398
>
> $SKU4
> ETS(A,A,N)
>
> Call:
> ets(y = x, model = "AZZ")
>
> Smoothing parameters:
> alpha = 1e-04
> beta = 1e-04
>
> Initial states:
> l = 566.9001
> b = -27.8818
>
> sigma: 127.2654
>
> AIC AICc BIC
> 167.3475 172.3475 169.6073
>
> Now when I run the same using:
> myfun<-function(x)
> {
> for(i in 1:length(x))
> {
> ets(x[i])
>>
>> }
> }
> I got the error as mentioned before. Now on modifying it to
> myfun<-function(x)
> {
> for(i in 1:length(x))
> {
> return(ets(x[[i]])
> }
> }
> I only got the output as
> ETS(A,N,N)
>
> Call:
> ets(y = x[[i]], model = "AZZ", opt.crit = c("amse"))
>
> Smoothing parameters:
> alpha = 0.3983
>
> Initial states:
> l = 516.188
>
> sigma: 181.8688
>
> AIC AICc BIC
> 172.6298 173.8298 173.7597
>
> I think its considering whole dataframe as a series.
Doubtful. It is quietly calculating all of the requested models but
you did not do anything with them inside the loop (which is a
function). You could have assigned them to something permanent or
printed them (or both):
ets_x <- list()
> for(i in 1:length(x))
> {
> print(ets(x[[i]]); ets_x <- c(ets_x, ets(x[[i]])
> }
> }
ets_x
> As said my objective it to essentially come up with a best
> exponential model
> for each of the SKU's in the dataframe. However I want to be able to
> extract
> information like mse, mape etc later. So kindly suggest.
>
> Thanks in advance,
> Phani
>
>
More information about the R-help
mailing list