[R] Passing Arguments in a function

Ista Zahn izahn at psych.rochester.edu
Tue Feb 15 16:25:26 CET 2011


Hi Michael,

On Tue, Feb 15, 2011 at 7:15 AM, Michael Pearmain
<michael.pearmain at gmail.com> wrote:
> Hi All,
>
> I'm having some trouble assigning arguments inside a function to
> produce a plot from a model
>
> Can anyone help me? Below I've outlined the situation and examples of
> failing and working code.
>
> Regards
>
> Mike
>
>
>
> ## data ##
>
> decay.data <- ...
>
>   behaviors     lift reach.uu estimated.conversions.uu total.reach
> 1          1 432.0083      770                      770        0.00
> 2          2 432.0083    29660                    29660        0.03
> 3          3 429.9864    30000                    29850        0.03
> 4          4 427.8599    30320                    30020        0.03
> 5          5 424.0105    30680                    30110        0.03
> 6          6 418.4710    31180                    30200        0.03
> 7          7 412.0022    31840                    30360        0.03
> 8          8 405.4553    32340                    30350        0.03
> 9          9 397.0083    33260                    30560        0.03
> 10        10 393.2382    33600                    30580        0.03
> 11        11 385.5431    34940                    31180        0.03
> 12        12 384.2942    35050                    31170        0.03
> 13        13 374.0299    36110                    31260        0.03
> 14        14 363.3057    37290                    31350        0.03
> 15        15 353.1185    38450                    31420        0.03
> 16        16 342.2190    40000                    31680        0.03
> 17        17 338.9232    40470                    31740        0.04
> 18        18 328.8666    41880                    31880        0.04
> 19        19 318.3219    45510                    33530        0.04
> 20        20 308.1200    47230                    33680        0.04
>
> If i use:
>
> library(nlrwr)
> # Build a model.
> decay.model <- nls(lift ~ SSexp(total.reach, y0, b), data = decay.data)
>
> # plot the model
> plot(decay.data[["total.reach"]], decay.data[["lift"]])
> xv <- seq(min(decay.data[["lift"]]), max(decay.data[["total.reach"]]), 0.02)
> yv <- predict(decay.model, newdata = list(total.reach = xv))
> lines(xv,yv)
>
> This works.

Actually it doesn't work for me. I get
xv <- seq(min(decay.data[["lift"]]), max(decay.data[["total.reach"]]), 0.02)
Error in seq.default(min(decay.data[["lift"]]),
max(decay.data[["total.reach"]]),  :
  wrong sign in 'by' argument

>
> If i try and wrap this in a function and pass the argument values i fail when
> i reach the "list(total.reach = xv)" i've tried various flavours or paste(),
> but again can't figure out where i am going wrong, any help appreciated.

Your example is not reproducible (you don't define the BuildDecayModel
function for us...) which makes it harder to help. But just by
eyeballing...

>
> PlotDecayModel <- function(x = "total.reach",
>                           y = "lift",
>                           data) {
>
>  decay.model <- BuildDecayModel(x= "total.reach",
Putting "total.reach" in quotes like this just sets x equal to the
text string "total.reach", not to the value of x passed to the
function. If you want the x to be passed from the function remove the
quotes.
>                                 y = "lift",
>                                 data = data)
>  # Plot the lift Vs reach plot.
>  plot(data[[x]], data[[y]])
>  # Add the model curve to the plot.
>  xv <- seq(min(data[[x]]), max(data[[x]]), 0.02)
>  yv <- predict(decay.model, newdata = list(x = xv))
>  lines(xv,yv)
> }
>
> I return the error
> Error in xy.coords(x, y) : 'x' and 'y' lengths differ
>
> I can see this is because the function ignores the  'newdata = list(x = xv)'
> as it is trying ot assign x on the data to be xv,
> (which doesn't exist in the model), so how can i use the arg "total.reach" so
> the argument 'newdata = list(x = xv)' is evaluated as
>     'newdata = list(total.reach = xv)'
I would try something like
nd <- list(xv)
names(nd) <- x
yv <- predict(decay.model, newdata = nd)
>
> Many thanks in advance

HTH, if you still have difficulty please post a reproducible example.

Best,
Ista

>
> Mike
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology
http://yourpsyche.org



More information about the R-help mailing list