[R] Passing Arguments in a function

Michael Pearmain michael.pearmain at gmail.com
Tue Feb 15 13:15:05 CET 2011


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.

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.

PlotDecayModel <- function(x = "total.reach",
                           y = "lift",
                           data) {

  decay.model <- BuildDecayModel(x= "total.reach",
                                 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)'

Many thanks in advance

Mike



More information about the R-help mailing list