I want to append sequentially my formula within a while loop so that after each iteration I get the previous value appended to the new value, for example:
i = 0
predictor = "test"
while(i < 7){
i = i 1
predictor <- reformulate(c(predictor, glue('I({predictor}^{i})')), response='go')
print(predictor) # Now we print predictor
}
[1] go ~ test I(test^1)
>Error in reformulate(c(predictor, glue("I({predictor}^{i})")), response = "go") :
'termlabels' must be a character vector of length at least one
Expected output:
[1] go ~ test I(test^1) I(test^2)
[1] go ~ test I(test^1) I(test^2) I(test^3)
[1] go ~ test I(test^1) I(test^2) I(test^3) I(test^4)
[1] go ~ test I(test^1) I(test^2) I(test^3) I(test^4) I(test^5)
[1] go ~ test I(test^1) I(test^2) I(test^3) I(test^4) I(test^5) I(test^6)
[1] go ~ test I(test^1) I(test^2) I(test^3) I(test^4) I(test^5) I(test^6) I(test^7)
Here is the working code that I used with the help of @onyambu :
poly66 <- function(data, response, predictor){
for(i in 1:3){
test <- reformulate(glue('poly({predictor},{i}, raw=TRUE)'), response=response)
lmod <- lm(test, data)
print(lmod)
}
}
poly66(savings, 'sr', 'pop15')
>Call:
lm(formula = test, data = data)
Coefficients:
(Intercept) poly(pop15, 1, raw = TRUE)
17.497 -0.223
Call:
lm(formula = test, data = data)
Coefficients:
(Intercept) poly(pop15, 2, raw = TRUE)1 poly(pop15, 2, raw = TRUE)2
22.399363 -0.522473 0.004268
Call:
lm(formula = test, data = data)
Coefficients:
(Intercept) poly(pop15, 3, raw = TRUE)1 poly(pop15, 3, raw = TRUE)2 poly(pop15, 3, raw = TRUE)3
-88.696453 9.808536 -0.305374 0.002995
CodePudding user response:
Since you start with predictor, you need to modify it rather than creating a new variable. Hence
i = 0
predictor = "test"
while(i < 7){
i = i 1
predictor <- c(predictor, i)
print(predictor) # Now we print predictor
}
Edit:
Depending on what exactly you are working on, you might find easier methods. Eg if you are fitting models, then you could simply use poly
with raw = TRUE
to get the desired results. ie:
test <- 1:4
poly(test,1,raw = TRUE)
1
[1,] 1
[2,] 2
[3,] 3
[4,] 4
attr(,"degree")
[1] 1
attr(,"class")
[1] "poly" "matrix"
poly(test,2,raw = TRUE)
1 2
[1,] 1 1
[2,] 2 4
[3,] 3 9
[4,] 4 16
attr(,"degree")
[1] 1 2
attr(,"class")
[1] "poly" "matrix"
poly(test, 3, raw = TRUE)
1 2 3
[1,] 1 1 1
[2,] 2 4 8
[3,] 3 9 27
[4,] 4 16 64
attr(,"degree")
[1] 1 2 3
attr(,"class")
[1] "poly" "matrix"
Notice how the degrees were added to you without physically creating the I(test^2), I(test^3)
variables.
In this case you will only change the degree.
If you are only interested in the formula, then consider using update
:
form <- go~test
i <-1
while(i < 7){
i = i 1
form <- update(form, sprintf('.~. I(test^%d)',i))
print(form)
}
go ~ test I(test^2)
go ~ test I(test^2) I(test^3)
go ~ test I(test^2) I(test^3) I(test^4)
go ~ test I(test^2) I(test^3) I(test^4) I(test^5)
go ~ test I(test^2) I(test^3) I(test^4) I(test^5) I(test^6)
go ~ test I(test^2) I(test^3) I(test^4) I(test^5) I(test^6)
I(test^7)
NOTE: --I am quite convinced you are fitting a model hence USE poly
: