[R] Fitting a GLM: Problems with ns & date functions

Cal calvin.jephcote at gmail.com
Wed Sep 28 10:41:53 CEST 2011

I am attempting to use R as part of a time-series analysis investigating the
influence of meteorological factors on health outcomes. The test csv dta
file that i am working with contains a complete daily set of  the variables
‘DATE’, ‘ADMINS’, ‘NOX’, ‘TEMP’ across a 5 year period (1827 days).

Within attempting to fit a GLM, I believe that I am having difficulty with
the ns function, although problems could also be arising through date
functions in R. Attached is my code and following errors, as a new starter
within the world of R any help would be much appreciated.

## [1] Original Analysis: FAILS

## packages loaded for use within my wider data analysis

## splines: For date there are 4 seasons across 5 years
## splines: For temp cold and warm seasons are 6 months long

fit <- glm(J00_99 ~ NOX_LIN + ns(DATE_B, 4 * 5) + ns(TEMP_LIN, 6), 
           data = data, family = poisson)
### Error in (1 - h) * qs[i] : non-numeric argument to binary operator

pr <- predict(fit, type = "terms")
### Error in predict(fit, type = "terms") : object 'fit' not found

## [2] Attempt 2: FAILS

##### So I have also been informed that when working with date i should use
the chron
##### package to convert the dates within my data file to a usable format.
This could be causing such problems

dates<-as.POSIXct(strptime(data[, "DATE"], format = "%d/%m/%Y", "GMT"))

fit <- glm(ADMINS ~ NOX + ns(date, 4 * 5) + ns(TEMP, 6), 
           data = data, family = poisson)

## Error in as.vector(x, mode) : cannot coerce type 'closure' to vector of
type 'any'

View this message in context: http://r.789695.n4.nabble.com/Fitting-a-GLM-Problems-with-ns-date-functions-tp3850379p3850379.html
Sent from the R help mailing list archive at Nabble.com.

More information about the R-help mailing list