Re: [R] Predict with loess

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Tue 13 Jun 2006 - 18:58:44 EST

On Tue, 13 Jun 2006, Jouanin Celine wrote:

> I want to do a nonparametric regression. Iím using the function loess.
> The variable are the year from 1968 to 1977 and the dependant variable is a proportion P. The dependant variable have missing value (NA).
> The script is :
>
> year <- 1969:2002
> length(year)
> [1] 34
>
> P <- c(NA,0.1,0.56,NA,NA,0.5,0.4,0.75,0.9,
> 0.98,0.2,0.56,0.7,0.89,0.3,0.1,0.45,0.46,0.49,0.78,
> 0.25,0.79,0.23,0.26,0.46,0.12,0.56,0.8,0.55,0.41,
> 0.36,0.9,0.22,0.1)
> length(P)
> [1] 34
>
> lo1 <- loess(P~year,span=0.3,degree=1)
> summary(lo1)
> yearCo <- 1969:2002
> year_lo <- data.frame(year = yearCo )
> length(year_lo)
> [1] 34

I get 1 here, and so should you.

> mlo <- predict(loess(P~year,span=0.3,degree=1),new.data=year_lo,se=T)

It should be newdata, not new.data

> mlo$fit
> mlo$se.fit

Notice that these are of length 31, not 34

You are trying to predict at the values used for fitting (possibly not what you intended), so you don't actually need this. Try

lo1 <- loess(P~year,span=0.3,degree=1, na.action=na.exclude) fitted(lo1)
plot(year,P,type='o')
lines(year, fitted(lo1))

Or if you want to try interpolation

lines(year, predict(lo1, newdata=year_lo))

This will not extrapolate to 1969, and as far as I recall the version of loess in R does not allow extrapolation.

> plot(year,P,type='o')
> lines(year,predict(loess(P~year,span=0.15,degree=1),new.data=year_lo,
> se=T,na.action=na.omit)$fit,col='blue',type='l')
>
> The message error indicates that x and y donít have the same length.

> In fact in m$fit and m$se.fit there are 3 values who donít have a
> fitted value.

Correct, and that's because you used na.action=na.omit and did not specify newdata.

[...]

-- 
Brian D. Ripley,                  ripley@stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595


______________________________________________ R-help@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

Received on Tue Jun 13 19:04:19 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Tue 13 Jun 2006 - 22:11:23 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.