开发者

Is there an implementation of loess in R with more than 3 parametric predictors or a trick to a similar effect?

开发者 https://www.devze.com 2023-03-13 04:51 出处:网络
Calling all experts on local regression and/or R! I have run into a limitation of the standard loess function in R and hope you have some advice. The current implementation supports only 1-4 predicto

Calling all experts on local regression and/or R!

I have run into a limitation of the standard loess function in R and hope you have some advice. The current implementation supports only 1-4 predictors. Let me set out our application scenario to show why this can easily become a problem as soon as we want to employ globally fit parametric covariables.

Essentially, we have a spatial distortion s(x,y) overlaid over a number of measurements z:

z_i = s(x_i,y_i) + v_{g_i}

These measurements z can be grouped by the same underlying undistorted measurement value v for each group g. The group membership g_i is known for each measurement, but the underlying undistorted measurement values v_g for the groups are not known and should be determined by (global, not local) regression.

We need to estimate the two-dimensional spatial trend s(x,y), which we then want to remove. In our application, say there are 20 groups of at least 35 measurements each, in the most simple scenario. The measurements are randomly placed. Taking the first group as reference, there are thus 19 unknown offsets.

The below code for toy data (with a spatial trend in one dimension x) works for two or three offset groups.

Unfortunately, the loess call fails for four or more offset groups with the error message

Error in simpleLoess(y, x, w, span, degree, parametric, drop.square,
normalize,  :
  only 1-4 predictors are allowed"

I tried overriding the restriction and got

k>d2MAX in ehg136.  Need to recompile with increased dimensions.

How easy would that be to do? I cannot find a definition of d2MAX anywhere, and it seems this might be hardcoded -- the error is apparently triggered by line #1359 in loessf.f

if(k .gt. 15)   call ehg182(105)

Alternatively, does anyone know of an implementation of local regression with global (parametric) offset groups that could be applied here?

Or is there a better way of dealing with this? I tried lme with correlation structures but that seems to be much, much slower.

Any comments would be greatly appreciated!

Many thanks,

David

###
#
# loess with parametric offsets - toy data demo
#

x<-seq(0,9,.1);
x.N<-length(x);

o<-c(0.4,-0.8,1.2#,-0.2  # works for three but not four
     );  # these are the (unknown) offsets
o.N<-length(o);
f<-sapply(seq(o.N),
          function(n){
            ifelse((seq(x.N)<= n   *x.N/(o.N+1) &
                    seq(x.N)> (n-1)*x.N/(o.N+1)),
                    1,0);
          });
f<-f[sample(NROW(f)),];

y<-sin(x)+rnorm(length(x),0,.1)+f%*%o;
s.fs<-sapply(seq(NCOL(f)),function(i){paste('f',i,sep='')});
s<-paste(c('y~x',s.fs),collapse='+');
d<-data.frame(x,y,f)
names(d)<-c('x','y',s.fs);

l<-loess(formula(s),parametric=s.fs,drop.square=s.fs,normalize=F,data=d,
         span=0.4);
yp<-predict(l,newdata=d);
plot(x,y,pch='+',ylim=c(-3,3),col='red');  # input data
points(x,yp,pch='o',col='blue');           # fit of that

d0<-d; d0$f1<-d0$f2<-d0$f3<-0;
yp0<-predict(l,newdata=d0);
points开发者_开发问答(x,y-f%*%o);     # spatial distortion
lines(x,yp0,pch='+');  # estimate of that

op<-sapply(seq(NCOL(f)),function(i){(yp-yp0)[!!f[,i]][1]});

cat("Demo offsets:",o,"\n");
cat("Estimated offsets:",format(op,digits=1),"\n");


Why don't you use an additive model for this? Package mgcv will handle this sort of model, if I understand your Question, just fine. I might have this wrong, but the code you show is relating x ~ y, but your Question mentions z ~ s(x, y) + g. What I show below for gam() is for response z modelled by a spatial smooth in x and y with g being estimated parametrically, with g stored as a factor in the data frame:

require(mgcv)
m <- gam(z ~ s(x,y) + g, data = foo)

Or have I misunderstood what you wanted? If you want to post a small snippet of data I can give a proper example using mgcv...?

0

精彩评论

暂无评论...
验证码 换一张
取 消