-2

I'm trying to create variables to use, for each variable it's called, M000 M001 M002 M003

Example

c.n_vars <- nrow(comb)
for (i in 1:c.n_vars)
{
paste("M",comb[i,1],comb[i,2],comb[i,3]) = Arima(y,order=c(arima[1,1],arima[2,1],arima[3,1]),seasonal=list(order=c(comb[i,1],comb[i,2],comb[i,3]),period=12))
}

where comb is all combinations

a <- c(0,1,2,3,4)
b <- c(0,1,2,3,4)
c <- c(0,1,2,3,4)
comb <- expand.grid(a, b,c)



row parameter1 parameter2 parameter3
1     0           0          0
2     1           0          0
3     2           0          0
4     3           0          0
5     4           0          0
6     0           1          0
7     1           1          0
8     2           1          0
9     3           1          0
10    4           1          0
11    0           2          0
12    1           2          0
13    2           2          0
14    3           2          0
15    4           2          0

and arima is

arima <- data.frame(c(2,1,4))

row parameters
1       2
2       1
3       4

i am trying to create

c.n_vars <- nrow(comb)
for (i in 1:c.n_vars)
{
paste("M",comb[i,1],comb[i,2],comb[i,3]) = Arima(y,order=c(arima[1,1],arima[2,1],arima[3,1]),seasonal=list(order=c(comb[i,1],comb[i,2],comb[i,3]),period=12))
}

this code must return

    for i = 1

    M000 = arima model saved in that variable

    for i = 2

    M100 = arima model saved in that variable

    for i = 3

    M200 = arima model saved in that variable
.
.
.
.
.
for i = 15

  M420 = arima model saved in that variable

and the following error appears

Error in paste("M", comb[i, 1], comb[i, 2], comb[i, 3]) = Arima(y, order = c(arima[1,  :  
     assignment target expands an object out of language

I need that each iteration of the variable 'i' be saved in a different variable

Is there any solution? or another way to do it

redondo
  • 107
  • 1
  • 1
  • 6
  • 1
    Ok first things first please provide some sample data – Bruno Dec 30 '19 at 15:01
  • 2
    What's the question exactly? Just how to change variable names? Any reason why `names(foo) <- c("blah", "blah2")` doesn't work? I don't get what the rest of the code has to do with that, or the error message – camille Dec 30 '19 at 15:06
  • 2
    You cannot assign to `paste()`. Have a look at this : [Create a variable name with “paste” in R?](https://stackoverflow.com/questions/5510966/create-a-variable-name-with-paste-in-r) – fabla Dec 30 '19 at 15:08
  • i use assign and throw the following error Error in optim(init[mask], armafn, method = optim.method, hessian = TRUE, : initial value in 'vmmin' is not finite – redondo Dec 30 '19 at 15:27

1 Answers1

1

Your sample code is still incomplete. I was not able to run it. For example y is missing.

As Base_R_Best_R pointed out, you cannot use paste to create variables like that. You can use the following pattern instead. Also note that I replaced paste() with paste0() to avoid spaces in the names:

result = list()
for (i in 1:c.n_vars)
{
  result[[paste0("M",comb[i,1],comb[i,2],comb[i,3])]] = Arima(y,order=c(arima[1,1],arima[2,1],arima[3,1]),seasonal=list(order=c(comb[i,1],comb[i,2],comb[i,3]),period=12))
}

Access your variables like this:

result$M100
BigFinger
  • 1,033
  • 6
  • 7