solanum
solanum

Reputation: 23

R formula syntax for quadratic function

I have a quadratic model that I am trying to fit with mgcv::gam, and I need some help in coding it as an r formula:

enter image description here

My attempt so far:

model <- mgcv::gam(W ~ 1 + z + I(0.5*z^2))

Do you think this is the correct syntax for the formula? That is, does the formula match the equation I am trying to use? I am not exactly sure whether the 0.5 is placed correctly.

Upvotes: 0

Views: 79

Answers (1)

Eonema
Eonema

Reputation: 1320

The 1 in the formula will be interpreted as an unknown intercept to be estimated, not as a constant of 1. I think W ~ -1 + offset(rep(1, length(z))) + z + I(0.5*z^2) will do what you want. The -1 specifies that there is no intercept and offset(rep(1, length(z))) serves as the 1 in your equation. This works for stats::glm, so it should also work for mcgv::gam according to the help page, but I'm not familiar with mcgv so there might be quirks I'm not aware of.

Upvotes: 3

Related Questions