Hello, I am trying to estimate a multivariate regression of Y on X with regression splines. Y is (nx1), and X is (nxd), with d>1. I assume the data is generated by some unknown regression function f(X), as in Y f(X) + u, where u is some well-behaved regression error. I want to estimate f(X) via regression splines (tensor product splines). Then, I want to get the predicted values for some new points W. To be concrete, here is an example of what I want: #dimensions of the model d=2 n=1000 #some random data X <- matrix(runif(d*n,-2,2),n,d) U <- rnorm(n) Y <- X[,1] + X[,2] + U # a new point for prediction W <- matrix(rep(0),1,d) Now if I wanted to use local polynomials instead of splines, I could load the 'locfit' package and run (something like): lp.results <- smooth.lf(X,Y,kern="epan",kt="prod",deg=1,alpha=c(0,0.25,0),xev=W,direct=TRUE)$y Or, if X was univariate (ie d=1), I could use (something like): spl.results <- predict(smooth.spline(X,Y, nknots=6),W) But smooth.spline only works for univariate data. I looked at the "crs" package, and it at least will fit the multivariate spline, but I don't see how to predict the new data from this. That is, I run a command like: spl.fit <- crs(Y~X[,1] + X[,2],basis="tensor", degree=c(3,3),segments=c(4,4),degree.min=3,degree.max=3, kernel=FALSE, cv="none",knots="uniform",prune=FALSE) Then what? What I really want is the spline version of the smooth.lf command above, or the multivariate version of smooth.spline. Any ideas/help? Thanks, Max
One possibility is.... library(mgcv) ## isotropic thin plate spline smoother b <- gam(Y~s(X[,1],X[,2])) predict(b,newdata=list(X=W)) ## tensor product smoother b <- gam(Y~te(X[,1],X[,2])) predict(b,newdata=list(X=W)) ## variant tensor product smoother b <- gam(Y~t2(X[,1],X[,2])) predict(b,newdata=list(X=W)) ... these would all result in penalized regression spline fits with smoothing parameters estimated (by GCV, by default). If you don't want penalization then use, e.g. s(X[,1],X[,2],fx=TRUE) to get pure regression spline (`k' argument to s, te and t2 controls spline basis dimension --- see docs). best, simon On 09/20/2011 03:11 PM, Max Farrell wrote:> Hello, > > I am trying to estimate a multivariate regression of Y on X with > regression splines. Y is (nx1), and X is (nxd), with d>1. I assume the > data is generated by some unknown regression function f(X), as in Y > f(X) + u, where u is some well-behaved regression error. I want to > estimate f(X) via regression splines (tensor product splines). Then, I > want to get the predicted values for some new points W. > > To be concrete, here is an example of what I want: > > #dimensions of the model > d=2 > n=1000 > #some random data > X<- matrix(runif(d*n,-2,2),n,d) > U<- rnorm(n) > Y<- X[,1] + X[,2] + U > # a new point for prediction > W<- matrix(rep(0),1,d) > > Now if I wanted to use local polynomials instead of splines, I could > load the 'locfit' package and run (something like): > > lp.results<- smooth.lf(X,Y,kern="epan",kt="prod",deg=1,alpha=c(0,0.25,0),xev=W,direct=TRUE)$y > > Or, if X was univariate (ie d=1), I could use (something like): > > spl.results<- predict(smooth.spline(X,Y, nknots=6),W) > > But smooth.spline only works for univariate data. I looked at the > "crs" package, and it at least will fit the multivariate spline, but I > don't see how to predict the new data from this. That is, I run a > command like: > > spl.fit<- crs(Y~X[,1] + X[,2],basis="tensor", > degree=c(3,3),segments=c(4,4),degree.min=3,degree.max=3, kernel=FALSE, > cv="none",knots="uniform",prune=FALSE) > > Then what? > > What I really want is the spline version of the smooth.lf command > above, or the multivariate version of smooth.spline. Any ideas/help? > > Thanks, > Max > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
Thank you for the reply, it looks like the second option (te) will work perfectly! Max On Tue, Sep 20, 2011 at 2:39 PM, Max Farrell <maxhf at umich.edu> wrote:> One possibility is.... > > library(mgcv) > > ## isotropic thin plate spline smoother > b <- gam(Y~s(X[,1],X[,2])) > predict(b,newdata=list(X=W)) > > ## tensor product smoother > b <- gam(Y~te(X[,1],X[,2])) > predict(b,newdata=list(X=W)) > > ## variant tensor product smoother > b <- gam(Y~t2(X[,1],X[,2])) > predict(b,newdata=list(X=W)) > > ... these would all result in penalized regression spline fits with > smoothing parameters estimated (by GCV, by default). If you don't want > penalization then use, e.g. s(X[,1],X[,2],fx=TRUE) to get pure > regression spline (`k' argument to s, te and t2 controls spline basis > dimension --- see docs). > > best, > simon > > On 09/20/2011 03:11 PM, Max Farrell wrote: >> Hello, >> >> I am trying to estimate a multivariate regression of Y on X with >> regression splines. Y is (nx1), and X is (nxd), with d>1. I assume the >> data is generated by some unknown regression function f(X), as in Y >> f(X) + u, where u is some well-behaved regression error. I want to >> estimate f(X) via regression splines (tensor product splines). Then, I >> want to get the predicted values for some new points W. >> >> To be concrete, here is an example of what I want: >> >> #dimensions of the model >> d=2 >> n=1000 >> #some random data >> X<- matrix(runif(d*n,-2,2),n,d) >> U<- rnorm(n) >> Y<- X[,1] + X[,2] + U >> # a new point for prediction >> W<- matrix(rep(0),1,d) >> >> Now if I wanted to use local polynomials instead of splines, I could >> load the 'locfit' package and run (something like): >> >> lp.results<- smooth.lf(X,Y,kern="epan",kt="prod",deg=1,alpha=c(0,0.25,0),xev=W,direct=TRUE)$y >> >> Or, if X was univariate (ie d=1), I could use (something like): >> >> spl.results<- predict(smooth.spline(X,Y, nknots=6),W) >> >> But smooth.spline only works for univariate data. I looked at the >> "crs" package, and it at least will fit the multivariate spline, but I >> don't see how to predict the new data from this. That is, I run a >> command like: >> >> spl.fit<- crs(Y~X[,1] + X[,2],basis="tensor", >> degree=c(3,3),segments=c(4,4),degree.min=3,degree.max=3, kernel=FALSE, >> cv="none",knots="uniform",prune=FALSE) >> >> Then what? >> >> What I really want is the spline version of the smooth.lf command >> above, or the multivariate version of smooth.spline. Any ideas/help? >> >> Thanks, >> Max >> >> ______________________________________________ >> R-help at r-project.org mailing list >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html >> and provide commented, minimal, self-contained, reproducible code. >> >