[R] Newbie question--locally weighted regression
Gregory Snow
Greg.Snow at intermountainmail.org
Wed Jan 4 16:47:37 CET 2006
Using a gam model (package gam, possibly others) will take care of the
link function (and variance function) for you and allow using loess to
fit the data. Here is a quick example to get you started, though you
should read up on gam models yourself as well.
library(gam)
x <- seq(0,1, length=250)
y <- rpois(250, (sin(x*2*pi)+1.2)*3)
plot(x,y)
lines(x,(sin(x*2*pi)+1.2)*3, col='blue')
fit <- gam(y~lo(x), family=poisson)
lines(x, predict(fit, data.frame(x=x), type='response'), col='green')
fit2 <- gam(y~lo(x, span=0.75, degree=2), family=poisson)
lines(x, predict(fit2, data.frame(x=x), type='response'), col='red')
Hope this helps,
--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at intermountainmail.org
(801) 408-8111
> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch
> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Thomas L Jones
> Sent: Wednesday, January 04, 2006 2:11 AM
> To: R-project help
> Subject: [R] Newbie question--locally weighted regression
>
>
> I have a dataset, a time series comprising count data at five
> minute intervals. These are the number of people who voted at
> a particular voting place during a recent election. The next
> step is to smooth the data and estimate a demand vs
> time-of-day function; the problem is of interest in
> preventing long lines at voting places. I am using the R
> Project software.
>
> However, I am not a statistician, and I am somewhat baffled
> by how to do the smoothing. These are integers with roughly
> Poisson distribution, and the use of a least-squares
> regression would create large errors. Apparently something
> called a "link function" factors into the equation somehow.
>
> Question: Do I want a link function? If so, do I want a
> logarithmic link function? Unless I change my mind, I will
> use lowess or loess for the smoothing; how do I tell it to
> use a link function?
>
> Doc
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>
More information about the R-help
mailing list