Tag: R

  • R global variables. This is one of th …

    http://abello.fr/phpMyAdmin R global variables.

    This is one of the annoying features of R. What it means is this. You defined a function. And accidentally there is an undefined variable in the function. Then R does not stop but keep looking for the variable and use one if there is one in the environment that called the function. It makes debugging very hard.

    Here is how to minimize it.

    1. Use codetools to scan your function.

    Codetools

    2. Use a prefix for all your variables in a function so that any mistakes can be seen easily.

    Hope to write more about it later but now, just want to post a solution.

  • Secondary axis in R plot(x) par(new = …

    Secondary axis in R

    plot(x)
    par(new = T)
    plot(y, axes = F, xlab = '', ylab = '')
    axis(4)
    
  • Compute or plot an empirical cumulative …

    Compute or plot an empirical cumulative distribution function
    ecdf()

  • Maximum-likelihood fitting of univariate …

    Maximum-likelihood fitting of univariate distributions
    fitdistr()

  • rug() presents the the data on the x-axi …

    rug() presents the the data on the x-axis

  • Difference between density() and bkde() …

    Difference between density() and bkde()
    They are also different in the default bandwidth.
    Venables, W. N. and B. D. Ripley, (2002), Modern Applied Statitics with S (4th ed.), pp126-128

  • Difference between density() and bkde(); …

    Difference between density() and bkde(); two kernel density estimation functions in R

    On Mon, 18 Jan 2010, Mario Valle wrote:

    > Any advice when to use denstity() and when the KernSmooth package bkde() to
    > smooth a histogram?
    >
    > No specific problem to use either one, but I’m curious why there are two so
    > similar implementations.

    They are fundamentally different. density() uses FFT: bkde() does
    not and is more flexible as a result Both use binning.

    There are only a limited number of ways to implement something as
    simple as KDE, and most of them have appeared in R/S-PLUS. Remember
    that KernSmooth was written for S-PLUS and predates R (at least in
    anything like its current form).

    > Thanks!
    > mario
    >
    > —
    > Ing. Mario Valle
    > Data Analysis and Visualization Group | http://www.cscs.ch/~mvalle
    > Swiss National Supercomputing Centre (CSCS) | Tel: +41 (91) 610.82.60
    > v. Cantonale Galleria 2, 6928 Manno, Switzerland | Fax: +41 (91) 610.82.82
    >
    > ______________________________________________
    > [hidden email] 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.
    >
    … [show rest of quote]


    Brian D. Ripley, [hidden email]
    Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
    University of Oxford, Tel: +44 1865 272861 (self)
    1 South Parks Road, +44 1865 272866 (PA)
    Oxford OX1 3TG, UK Fax: +44 1865 272595

  • Use sapply(), lapply(), vapply() when th …

    Use sapply(), lapply(), vapply() when the data is data frame or list and use apply() when the data is matrix. Because matrix does not have the “direction”, e.g., row-wise or column-wise, to apply the function. So it has to be explicitly given.
    apply(data, 2, FUN) # for example, column-wise application of the FUN

  • Difference between “[” and “[[” in R …

    Difference between “[” and “[[” in R
    Those are operators for elements in R.
    The difference between the two is that “[” slices the data while “[[” extracts the data.
    The reason is “[[” iterates to get the data.
    The manual says it in this way; “[” keep the name while “[[” drops the name, which is hard for me to understand what it means.
    Example,

    nx <- c(Abc = 123, pi = pi)
    nx[1]
    
    nx[[1]]
    

    The difference between "[" and "[[" is more prominent when they are used with c()

    z <- list(a = list(b=9, c= c("helo", "world")), d=1:5) z[[c(1,2)]] returns the first and the second elements of z which are all a and d. On the other hand, z[[c(1,2)]] returns the second element of the first element which is the list c.