Brent,
This example works for me:
set.seed(1)
tmp <- data.frame(x = sort(runif(100)))
tmp$y <- cos(2 * pi * tmp$x) + exp(tmp$x) + rnorm(nrow(tmp))
fit <- loess(y ~ x, data = tmp, span = 0.1)
rss <- sum(residuals(fit)^2)
for(sp in seq(0.2, 1, 0.1)) {
fit <- update(fit, span = sp)
rss <- c(rss, sum(residuals(fit)^2))
}
> rss
[1] 80.09872 91.25454 95.92742
[4] 99.30498 100.07263 101.45110
[7] 102.62999 103.22769 103.44736
[10] 103.62163
> version
Professional Edition Version 6.2.1 for Microsoft Windows : 2003
As expected the smallest span produces the smallest rss. In your example
you are using loess.calibration (not loess.model) to extract the
residuals. Are you sure you are doing the assignment to the correct object?
Best,
Sundar
Myers, Brent wrote:
Sundar,
You are correct, in the example I posted I forgot the assignment, but it
doesn't matter. For instance, whether you use the update or just the
model statement (with the appropriate assignment, the RSS does not
change. Run manually, the value does (of course) differ. Is there a
problem with the function accepting the i-th iteration value.
loess.model <- update(loess.model, span = i) . . . still doesn't work.
Regards,
Brent
-----Original Message-----
From: Sundar Dorai-Raj [mailto:sundar.dorai-raj@PDF.COM]
Sent: Tuesday, February 24, 2004 1:50 PM
To: Myers, Brent
Cc: s-news@lists.biostat.wustl.edu
Subject: Re: [S] Loess loop
Brent,
You didn't assign the update call to anything:
for(i in ivect) {
loess.calibration <- update(loess.model, span = i)
span.out <- c(span.out, i)
rss <- sum(loess.calibration$residuals^2)
rss.out <- c(rss.out, rss)
}
Best,
Sundar
Myers, Brent wrote:
Group,
What is it I'm not understanding here?
The following code fits a loess model at a range of spans, however
residual sum of squares is not changing.
It makes no difference if the model is inside the loop or if update is
used.
How could apply be used instead?
> loess.model <- loess(formula = Clay ~ CMDivergence, data =
Soil.Profile.Data, subset = Study == "Calibration", na.action =
na.exclude, span = i, degree = 2, normalize = T, family
= "gaussian")
> span.out <- 0
> rss.out <- 0
> ivect <- seq(0.1, 1, by = 0.1)
> for(i in ivect) {
update(loess.model, span = i)
span.out <- c(span.out, i)
rss <- sum(loess.calibration$residuals^2)
rss.out <- c(rss.out, rss)
}
> out.stats.loess <- data.frame(span.out, rss.out)
> print(out.stats.loess)
span.out rss.out
1 0.0 0.00
2 0.1 32975.69
3 0.2 32975.69
4 0.3 32975.69
5 0.4 32975.69
6 0.5 32975.69
7 0.6 32975.69
8 0.7 32975.69
9 0.8 32975.69
10 0.9 32975.69
11 1.0 32975.69
> rm(span.out, rss.out)
D. Brenton Myers
Research Specialist
University of Missouri - Agronomy / USDA-ARS-ASEQ
158 Agriculture Engineering Building
Columbia, MO 65211
(573) 882-1146 wk
(573) 228-0929 cell
myersdb@missouri.edu
|