I'm running a cross-validation algorithm to find the best polynomial fit for data that changes from day to day. I want to find a non-cumbersome method of displaying the fit in a simple plot without having to manually write the whole regression formula and the beta coefficients for the plotting each time. For the regression formula the solve is easy, I create a string using sprintf and use as.formula() on the string .
The problem is plotting the line. I create a string in the same way, but the as.formula() function seems only to work for regression formulas, not formulas on the form "beta + beta*t". I have also tried using eval(parse()) as shown below, but this only creates a vector of NA's.
#Create strings
poly_form = "y ~ t"
beta_form = "beta[1]"
for (i in 1:pmin) { #pmin is the best polynomial fit, e.g. 4 or 9.
poly_form <- sprintf("%s + I(t^%s)", poly_form, i)
beta_form <- sprintf("%s + beta[%s]*t^%s",beta_form, i+1, i)
}
#Regression
poly.mod = lm(as.formula(poly_form))
beta = coef(poly.mod)
#Plot
plot(t, y, type = 'h')
lines(t, eval(parse(text = beta_form))) #This doesn't work.
So in essence, how can I use the string I've created as part of an input into the lines function in a way that automatically produces the same output as this:
lines(t, beta2[1] + beta2[2]*t + beta2[3]*t^2 + beta2[4]*t^3 + beta2[5]*t^4 + beta2[6]*t^5 + beta2[7]*t^6)