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
Rafael Jimenez wrote:
>Dear list,
>
>I have been trying to use the function outer to make contour plots of a
>cost function for regression analysis with no success. (see commented
>code below).
>
>Could anybody give me some advice on what I am doing wrong, or what is
>relevant for me to read?
>
>Thanks,
>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
>
>