# [R] function design

Michaell Taylor pols1oh at bestweb.net
Tue Mar 19 01:29:54 CET 2002

```>
>   Why not tell us what you actually want to do?
>   There are probably much simpler, more direct ways of doing it, but
>   given the information above who could tell?
>   For example, I don't think I have needed to use <<- to overcome any
>   programming problem that did not involve environments.

You are right.  Frequently the simplifications don't actually target the
problem.  The overall issue is that the data.frame data is a collection
of monte carlo simulations.  the ML data is a most likely estimate of
the same series. I want to adjust the Monte carlo simulations so that
the most likely fit in the "fat part" of the montecarlo distribution. I
do this by assessing the year by year fit and make the adjustments -
purposes.

lagger _ function(X,y,z,l=1) {temp _
c(rep(NA,l),X[-((length(X)-(l-1)):length(X))])
temp[y==min(y)] _ z
return(temp)}
# X,y,z,l = variable, marker for first obs of scenario (yr), z=prior
level value
growit _ function(x,y,z) {(x/lagger(x,y,z)) }

inrange _ function(v,Y,s,z,mlv,met){
lower _ mean(v[data\$yr==Y])-(var(v[data\$yr==Y])^.5)
upper _ mean(v[data\$yr==Y])+(var(v[data\$yr==Y])^.5)
MLE _ mlv[ML\$yr==Y & ML\$metcode==met]
diffa _ 0
if (MLE<lower) {print('lower')
diffa <- lower-MLE
v[data\$yr==Y] <<- v[data\$yr==Y]-diffa} THE PROBLEM AREA
if (MLE>upper) {print('higher')
diffa <- MLE-upper
v[data\$yr==Y] <<- v[data\$yr==Y]+diffa} THE PROBLEM AREA
if (diffa!=0) {
v _ growit(v,s,z)
}
}
inrange(data\$Linv,Y,data\$yr,starting.inv,ML\$inv,met)

THE PROBLEM AREA - marks the location where I would like to save the
changes out to the main data stream.

Hope this helps.

Thanks.

>
>   deparse(substitute(x)), will however, get you the name of the
>   variable that was supplied as x.
>
>   the following, is a vectorized version of something similar
>
>
>   foo <-function(x,y) ifelse(x>y,x,y)
>   a <- foo(a,b)
>
> >
> > Michaell
> >
> >
> > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> > 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
> > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
>
> --
> +---------------------------------------------------------------------------+
> | Robert Gentleman                 phone : (617) 632-5250                   |
> | Associate Professor              fax:   (617)  632-2444                   |
> | Department of Biostatistics      office: M1B28
> | Harvard School of Public Health  email: rgentlem at jimmy.dfci.harvard.edu   |
> +---------------------------------------------------------------------------+

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

```