Skip to content

plot multivariate function with known coefficient.

2 messages · YIHSU CHEN, Gavin Simpson

#
Dear R users --

I have a simple question that I cannot find the answers in the archive
email:  how to plot a multivariate function if I know the coefficients
already?  For instance, if I like to plot a 3D fig of Pi against X and
Y with the function Pi = a0 + a1X + a2Y, where a0, a1 & a2 are known.
Can "curve" do this?  So far, I have only seen univariate case.

Thank you.

Yihsu Chen
#
On Tue, 2008-12-09 at 16:07 -0800, YIHSU CHEN wrote:
Not sure about a "curve3D()" but you can do this yourself using
expand.grid (to give combinations of X and Y you wish to evaluate Pi at)
and then use the vectorised nature of R to compute Pi for the
combinations given known coefficients.

E.g.:

X <- seq(1, 100, by = 5)
Y <- seq(1, 100, by = 5)
XY <- expand.grid(X = X, Y = Y)
a0 <- 2
a1 <- 0.5
a2 <- -0.3
Pi <- with(XY, a0 + (a1 * X) + (a2 * Y))
Pi <- matrix(Pi, ncol = length(X), byrow = TRUE)
persp(X, Y, Pi)


HTH

G