# [R] Why doesn't outer work?

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Mon Nov 5 12:03:53 CET 2001

```"Rees, Mark" <m.rees at ic.ac.uk> writes:

> Hello
>
> I'm a population ecologist and use R for all my stats and modelling.
> Recently I have been using R to numerically solve integral projection
> models. This involves constructing several large matrices. The current code
> by Easterling (Size-specific sensitivity: Applying a new structured
> population model. Ecology, 2000, 81, 694-708) uses nested loops to construct
> the matrices. To speed up the code I decided to replace these with outer
> functions. One of these works just as expected and the other almost works!
> Having spent 2 days trying to work out what's going wrong I have decided to
> ask for help. The R-code is given below, the first part sets various
> parameters, then there are several functions. The main functions are called
> bigmatrix and new.bigmatrix which construct the matrices, these should give
> the same answers, as all I've done is replace the nested loops with outer
> functions. The Bmatrix calculation is correct but the Pmatrix calculation
> gets some of the answers wrong. The final lines of code test the functions.
>
> Any advice on what's going wrong would be greatly appreciated, also any tips
> for speeding up the code would fantastic.

(In general, it is easier to answer queries if you also tell us what
is going wrong...)

The standard way of messing up outer() is to pass it a function that
doesn't vectorize. As far as I can see, your sx() will sometimes
return a scalar.

BTW: t(outer(y,y,...)) is transposing a symmetric matrix?

--
O__  ---- Peter Dalgaard             Blegdamsvej 3
c/ /'_ --- Dept. of Biostatistics     2200 Cph. N
(*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

```