I’m trying to use the ns() function from the splines package with a poisson GLM I am using to test for significance of particulate matter concentration (pm.lag0) on health outcomes (Freq):
> gfit4 = glm(Freq ~ pm.lag0 + ns(date, df=2), family = poisson(),
data = dt, offset = log(pop))
I get these errors back:
Error in splineDesign(knots, x, ord, derivs, outer.ok = outer.ok) :
must have at least 'ord' knots
In addition: Warning message:
In sort(as.numeric(knots)) : NAs introduced by coercion
Is that not a valid use of ns()? Can someone help me decode this error message? The splines documentation that R provides doesn’t seem to match this error (?ns).
I can’t see a reason why, in principle, it is not possible to use
ns()inglm(). To see why, study whatns()does in a formula. From?nsWhich shows that it provides the B-spline basis for the natural spline of the
heightvariable. Nothing special here.Hence I suspect your
datevariable is not numeric or not something that R could work with by coercing it to be numeric without introducingNA– see the warning message. Without a reproducible example and information on your data it is impossible to tell however!In addition, you might want to look at the
gam()function in package mgcv which as a Recommended package is distributed with R. It is designed to fit semi-parametric models in the manner you describe and can include parametric terms as well as smooths/splines of other terms. The package is fairly comprehensive and can fit a large number of types of spline. See it’s manual.