English 中文(简体)
利用R的Plyr, 功能复杂,可回归多种变量
原标题:Using Plyr in R with a complex function that returns multiple variable
  • 时间:2011-09-08 05:04:10
  •  标签:
  • r
  • plyr

我有一套数据集,有三种分类变量:状况、次和次;延迟。 这里是我的数据的简化版本(实时数据更长)。

sub condition delay later_value choiceRT later_choice primeRT        cue
 10      SIZE    10          27     1832            1     888      CHILD
 10      PAST     5          11      298            0    1635      PANTS
 10      SIZE    21          13      456            0     949      CANDY
 11      SIZE   120          22      526            1    7963        BOY
 11    FUTURE   120          27      561            1    4389   CHILDREN
 11      PAST     5          13      561            1    2586     SPRING

I have a complicated set of procedures to apply to these data (details are not important) I wrote the following function that accomplishes what I want when split by the three grouping variables. It returns 3 variables that I am interested in (indiff, p_intercept, & p_lv)

 getIndiffs <- function(currdelay){
      if (mean(currdelay$later_choice) == 1) {
        indiff = 10.5
        p_intercept = "laters"
        p_lv = "laters"
      }

      else if (mean(currdelay$later_choice) == 0) {
        indiff = 30.5

        # no p-val here, code that this was not calculated
        p_intercept = "nows"
        p_lv = "nows"
      }

      else {
        F <- factor(currdelay$later_choice)

        fit <- glm(F~later_value,data=currdelay,family=binomial())
        indiff <- -coef(fit)[1]/coef(fit)[2]

        if (indiff < 10) indiff = 10.5
        else if (indiff > 30) indiff = 30.5

        p_intercept = round(summary(fit)$coef[, "Pr(>|z|)"][1],3)
        p_lv = round(summary(fit)$coef[, "Pr(>|z|)"][2], 3)
        c(indiff,p_intercept,p_lv)
      }

我正试图 d忙地将数据应用于三个组别变量的每一次数据:

ddply (data,.(sub,condition,delay),getIndiffs)

然而,当我执政时,我发现错误。

Error in list_to_dataframe(res, attr(.data, "split_labels")) : Results do not have equal lengths

奇怪的是,当我只使用1组变数,但用2+ error倒错误时,这种工程就会被罚款。

Also, when I "simulate" splitting the dataset myself into a data drame only containing a subset split by the 3 grouping variables, my function works just fine. (Note: I ve tried different ways of returning 3 variables or even returning just 1 variable and it does not work, either)

基本上,我想知道的是,如何利用电压器来利用功能来回多个变量。

对我的问题的任何其它解决办法也受到欢迎。

问题回答

当我的职能适用于我的一个零件时,这种错误通常会发生到我身上。 无论如何,用<代码>dlply而不是dply,并审查产出;例如,

x <- dlply(data,.(sub,condition,delay),getIndiffs)
sapply(x,ncol)

检查其所有栏号相同。 否则,你的职能就更加标准化。

它像你的职能getIndiffs设计成单行,而不是整个数据范围。 <代码>d*ply(x,vars,fn) Handfn( a alldata framework, 包括配对该组意见的子集。 Hm, 也可以在三个不同地点——每个有条件条款结尾——恢复这一职能。 我认为,你打算在最后<代码>>>><>>>/代码>之后插入<代码>c(indiff,p_intercept,p_lv>。 (并用另一个<代码>}结束您的职能。)





相关问题
How to plot fitted model over observed time series

This is a really really simple question to which I seem to be entirely unable to get a solution. I would like to do a scatter plot of an observed time series in R, and over this I want to plot the ...

REvolution for R

since the latest Ubuntu release (karmic koala), I noticed that the internal R package advertises on start-up the REvolution package. It seems to be a library collection for high-performance matrix ...

R - capturing elements of R output into text files

I am trying to run an analysis by invoking R through the command line as follows: R --no-save < SampleProgram.R > SampleProgram.opt For example, consider the simple R program below: mydata =...

R statistical package: wrapping GOFrame objects

I m trying to generate GOFrame objects to generate a gene ontology mapping in R for unsupported organisms (see http://www.bioconductor.org/packages/release/bioc/vignettes/GOstats/inst/doc/...

Changing the order of dodged bars in ggplot2 barplot

I have a dataframe df.all and I m plotting it in a bar plot with ggplot2 using the code below. I d like to make it so that the order of the dodged bars is flipped. That is, so that the bars labeled "...

Strange error when using sparse matrices and glmnet

I m getting a weird error when training a glmnet regression. invalid class "dgCMatrix" object: length(Dimnames[[2]]) must match Dim[2] It only happens occasionally, and perhaps only under larger ...

Generating non-duplicate combination pairs in R

Sorry for the non-descriptive title but I don t know whether there s a word for what I m trying to achieve. Let s assume that I have a list of names of different classes like c( 1 , 2 , 3 , 4 ) ...

Per panel smoothing in ggplot2

I m plotting a group of curves, using facet in ggplot2. I d like to have a smoother applied to plots where there are enough points to smooth, but not on plots with very few points. In particular I d ...

热门标签