Hi, I've got a matrix, Z, of values representing (as it happens) optical power at each pixel location. Since I know in advance I've got a single, convex peak, I would like to do a 2D parabolic fit of the form Z = poly((x+y),2) where x and y are the x,y coordinates of each pixel (or equivalently, the row, column numbers). Is there an R function that lets me easily implement that? I've started down the path of something like zvec <- as.vector(Z), and creating applicable x,y vectors by something like (where for the sake of argument Z is 128x128) foo<-matrix(seq(1,128),128,128) xvec <- as.vector(foo) yvec <- as.vector(t(foo)) at which point I can feed zvec, xvec, yvec to lm() . I'm hopeful someone can point me to a much easier way to do the same thing. Oh, and if there's a 2-D splinefunction generator, that would work for me as well. thanks Carl
You may want to consider spatial::surf.ls Or, a simplistic approach where you fit a model such as using `lm': E[Z | x, y] = a + b(x - x0)^2 + c(y - y0)^2 where (x0, y0) is the location of maximum. Ravi. ________________________________________ From: r-help-bounces at r-project.org [r-help-bounces at r-project.org] On Behalf Of Carl Witthoft [carl at witthoft.com] Sent: Monday, May 02, 2011 7:14 PM To: r-help at r-project.org Subject: [R] easy way to do a 2-D fit to an array of data? Hi, I've got a matrix, Z, of values representing (as it happens) optical power at each pixel location. Since I know in advance I've got a single, convex peak, I would like to do a 2D parabolic fit of the form Z = poly((x+y),2) where x and y are the x,y coordinates of each pixel (or equivalently, the row, column numbers). Is there an R function that lets me easily implement that? I've started down the path of something like zvec <- as.vector(Z), and creating applicable x,y vectors by something like (where for the sake of argument Z is 128x128) foo<-matrix(seq(1,128),128,128) xvec <- as.vector(foo) yvec <- as.vector(t(foo)) at which point I can feed zvec, xvec, yvec to lm() . I'm hopeful someone can point me to a much easier way to do the same thing. Oh, and if there's a 2-D splinefunction generator, that would work for me as well. thanks Carl ______________________________________________ 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.
Hi Carl, Here is another slightly different (not necessarily the easiest) approach that uses a profiling technique. An advantage is that you get the maximum location directly. n <- 20 x <- sort(rnorm(n)) y <- sort(rnorm(n)) xy <- expand.grid(x, y) zfn <- function(x) 0.5 - 2.2 * (x[1] - 0.5)^2 - 0.9 * (x[2] + 0.5)^2 z <- rep(NA, length=n^2) for (i in 1:nrow(xy)) z[i] <- zfn(xy[i, ]) z <- z + rnorm(n^2, sd=0.3) obj <- function(par, x, y, z) { -summary(lm(z ~ I((x - par[1])^2) + I((y - par[2])^2)))$r.sq } require(dfoptim) ans <- nmk(par=colMeans(xy), fn=obj, x=xy[,1], y=xy[,2], z=z) ans$par # location of the maximum summary(lm(z ~ I((xy[,1] - ans$par[1])^2) + I((xy[,2] - ans$par[2])^2))) Ravi. ________________________________________ From: r-help-bounces at r-project.org [r-help-bounces at r-project.org] On Behalf Of Carl Witthoft [carl at witthoft.com] Sent: Monday, May 02, 2011 7:14 PM To: r-help at r-project.org Subject: [R] easy way to do a 2-D fit to an array of data? Hi, I've got a matrix, Z, of values representing (as it happens) optical power at each pixel location. Since I know in advance I've got a single, convex peak, I would like to do a 2D parabolic fit of the form Z = poly((x+y),2) where x and y are the x,y coordinates of each pixel (or equivalently, the row, column numbers). Is there an R function that lets me easily implement that? I've started down the path of something like zvec <- as.vector(Z), and creating applicable x,y vectors by something like (where for the sake of argument Z is 128x128) foo<-matrix(seq(1,128),128,128) xvec <- as.vector(foo) yvec <- as.vector(t(foo)) at which point I can feed zvec, xvec, yvec to lm() . I'm hopeful someone can point me to a much easier way to do the same thing. Oh, and if there's a 2-D splinefunction generator, that would work for me as well. thanks Carl ______________________________________________ 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.