# Nested Loop for summation

See the FAQ: How to do a minimal reproducible example reprex for beginners. The idea is to have a cut and paste example that will load all needed libraries and data to illustrate the problem. Without it, only general guidance can be offered.

The following example shows stepwise development of the solution to this type of problem. Below, it was easy to see that with the presence of y[x-2] in the function that the for loop would have to start with 3. I suspect a similar approach will allow you to get the logic right in your case.

# Simulate y
y <- numeric(100)
e <- rnorm(100)
for(i in 2:100)
y[i] <- 0.6*y[i-1] + e[i]

# define constants

a <- pi
phi.1 <- 0.6
phi.2 <- 0.2

# hardwired function to make illustration easier

make_term <- function(x) a + phi.1 * y[x-1] + phi.2 * y[x-2]

# illustrate application

holder <- list()
for (i in 3:length(y)) holder[i] = make_term(i)
unlist(holder)
#>  [1] 2.6234268 2.3265332 2.0950606 2.7646958 2.8868463 2.9229873 2.9721795
#>  [8] 4.1038078 4.9533835 4.8940731 4.7022580 4.3387726 3.9995287 2.7366209
#> [15] 2.4299101 1.5431100 0.3188871 0.8187863 2.1499154 2.6044797 2.1201821
#> [22] 1.4736645 2.7649998 3.1557277 2.7981470 2.1823276 2.5958512 3.1551375
#> [29] 4.0576772 3.7542651 3.5927147 3.0292660 3.7635644 3.3224826 3.1727496
#> [36] 3.5903654 3.6305808 3.7284071 2.3771208 2.1907763 1.9172209 1.4377688
#> [43] 2.3065391 3.0730440 3.3824714 3.5517975 3.0713545 3.9001606 3.5860109
#> [50] 3.4592384 3.0830691 3.1155525 3.2328833 3.6112411 3.7630496 2.5719653
#> [57] 2.6623233 2.6805534 3.2577633 3.1121410 3.0978636 3.4197126 3.8561072
#> [64] 3.4222195 4.0599125 5.1739849 4.3072138 3.4602369 2.9775378 2.2128272
#> [71] 1.9474705 2.7053496 3.3890641 3.6145090 3.4878419 2.5550897 3.4021988
#> [78] 3.4250704 4.3320918 3.7166646 3.3404745 3.3253659 3.8951965 4.6573359
#> [85] 4.6854418 3.7268465 2.7618039 2.2864325 3.1298235 4.5472345 4.9670926
#> [92] 4.3350595 3.8169973 3.6981643 3.6270248 3.6055920 3.4311680 2.7240743