[R] Use of outer
Rafael Jimenez
jimenez at ce.berkeley.edu
Mon Oct 13 05:50:06 CEST 2003
On Sun, 12 Oct 2003, Spencer Graves wrote:
> I get
>
> > var(as.vector(z.matrix))
> [1] 0
>
> I got sensible results from the following:
>
> z.matrix <- outer(theta.1, theta.2, "+")
> >
> > ## Plot
> > contour(theta.1, theta.2, z.matrix)
>
> hope this helps. spencer graves
Thanks for the pointer.
A constant matrix is obtained with my call to 'outer'. However, the call
to outer seems to work fine when a function that takes two arguments
only is used. I guess the problem is in my definition of the function
'J', which depends on four arguments, but I couldn't figure out how to
make it work.
-Rafael
[...]
> >## ============================================= CODE
> >## Create vectors and matrix
> >Y <- c(0.5116403,2.4055245,1.6596707,1.8286057,2.6119199)
> >
> >col.1 <- c(0.8997692,0.8216292,0.6449104,0.8179743,0.6602276)
> >col.2 <- c(0.01175669,0.89389797,0.19913807,0.29872301,0.66144258)
> >
> >X <- cbind(col.1, col.2)
> >
> >## Define function
> >## Give default values so that it "takes two arguments"
> >J <- function (x1, x2, X.val=X, Y.val=Y) {
> > t(Y.val-X.val[,1]*x1-X.val[,2]*x2) %*% (Y.val-X.val[,1]*x1-X.val[,2]*x2)
> >}
> >
> >## Get grid to plot
> >theta.1 <- array(seq(-1, 1, length=10))
> >theta.2 <- theta.1
> >
> >## How to do this with outer?
> >z.matrix <- outer(theta.1, theta.2, "J")
> >
> >## Plot
> >contour(theta.1, theta.2, z.matrix)
> >
> >______________________________________________
> >R-help at stat.math.ethz.ch mailing list
> >https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> >
> >
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
>
More information about the R-help
mailing list