Skip to content
Prev 47158 / 63424 Next

Dynamic list creation (SEXP in C) returns error "unimplemented type (29) in 'duplicate'"

Hi!

I didn't wanted to do this but I think that this is the easiest way
for you to understand my problem (thanks again for all the comments
that you have made). Here is a copy of the function that I'm working
on. This may be tedious to analyze, so I understand if you don't feel
keen to give it a time. Having dedicated many hours to this (as a new
user of both C and R C API), I would be very pleased to know what am I
doing wrong here.

G0 is a Nx2 matrix. The first column is a group id (can be shared with
several observations) and the second tells how many individuals are in
that group. This matrix can look something like this:

id_group  nreps
1  3
1  3
1  3
2  1
3  1
4  2
5  1
6  1
4  2
...

L0 is list of two column data.frames with different sizes. The first
column (id) are row indexes (with values 1 to N) and the second column
are real numbers. L0 can look something like this
[[1]]
id  lambda
3  0.5
15  0.3
25  0.2
[[2]]
id  lambda
15  0.8
40  0.2
...
[[N]]
id  lambda
80  1

TE0 is a int scalar in {0,1,2}

T0 is a dichotomous vector of length N that can look something like this
[1] 0 1 0 1 1 1 0 ...
[N] 1

L1 (the expected output) is a modified version of L0, that, for
instance can look something like this (note the rows marked with "*")

[[1]]
id  lambda
3  0.5
*15  0.15 (15 was in the same group of 50, so I added this new row and
divided the value of lambda by two)
25  0.2
*50  0.15
[[2]]
id  lambda
15  0.8
40  0.2
...
[[N]]
id  lambda
*80  0.333 (80 shared group id with 30 and 100, so lambda is divided by 3)
*30  0.333
*100 0.333

That said, the function is as follows

SEXP distribute_lambdas(
  SEXP G0,  // Groups ids (matrix of Nx2). First column = Group Id,
second column: Elements in the group
  SEXP L0,  // List of N two-column dataframes with different number of rows
  SEXP TE0, // Treatment effect (int scalar): ATE(0) ATT(1) ATC(2)
  SEXP T0   // Treat var (bool vector, 0/1, of size N)
)
{

  int i, j, l, m;
  const int *G = INTEGER_POINTER(PROTECT(G0 = AS_INTEGER(G0 )));
  const int *T = INTEGER_POINTER(PROTECT(T0 = AS_INTEGER(T0 )));
  const int *TE= INTEGER_POINTER(PROTECT(TE0= AS_INTEGER(TE0)));
  double *L, val;
  int *I, nlambdas, nreps;

  const int n = length(T0);

  PROTECT_INDEX pin0, pin1;
  SEXP L1;
  PROTECT(L1 = allocVector(VECSXP,n));
  SEXP id, lambda;

  // Fixing size
  for(i=0;i<n;i++)
  {
    SET_VECTOR_ELT(L1, i, allocVector(VECSXP, 2));
  //  SET_VECTOR_ELT(VECTOR_ELT(L1,i), 0, NEW_INTEGER(100));
  //  SET_VECTOR_ELT(VECTOR_ELT(L1,i), 1, NEW_NUMERIC(100));
  }

  // For over the list, i.e observations
  for(i=0;i<n;i++)
  {

    R_CheckUserInterrupt();

    // Checking if has to be analyzed.
    if (
      ((TE[0] == 1 & !T[i]) | (TE[0] == 2 & T[i])) |
      (length(VECTOR_ELT(L0,i)) != 2)
    )
    {
      SET_VECTOR_ELT(L1,i,R_NilValue);
      continue;
    }

    // Checking how many rows does the i-th data.frame has
    nlambdas = length(VECTOR_ELT(VECTOR_ELT(L0,i),0));

    // Pointing to the data.frame's origianl values
    I = INTEGER_POINTER(AS_INTEGER(PROTECT(VECTOR_ELT(VECTOR_ELT(L0,i),0))));
    L = NUMERIC_POINTER(AS_NUMERIC(PROTECT(VECTOR_ELT(VECTOR_ELT(L0,i),1))));

    // Creating a copy of the pointed values
    PROTECT_WITH_INDEX(id   = duplicate(VECTOR_ELT(VECTOR_ELT(L0,i),0)), &pin0);
    PROTECT_WITH_INDEX(lambda=duplicate(VECTOR_ELT(VECTOR_ELT(L0,i),1)), &pin1);

    // Over the rows of the i-th data.frame
    nreps=0;
    for(l=0;l<nlambdas;l++)
    {
      // If the current lambda id is repeated, ie ther are more individuals
      // with the same covariates, then enter.
      if (G[n+I[l]-1] > 1)
      {
        /* Changing the length of the object */
        REPROTECT(SET_LENGTH(id,    length(lambda) + G[n+I[l]-1] -1), pin0);
        REPROTECT(SET_LENGTH(lambda,length(lambda) + G[n+I[l]-1] -1), pin1);

        // Getting the new value
        val = L[l]/G[n+I[l] - 1];
        REAL(lambda)[l] = val;

        // Looping over the full set of groups
        m = -1,j = -1;
        while(m < (G[n+I[l]-1] - 1))
        {
          // Looking for individuals in the same group
          if (G[++j] != G[I[l]-1]) continue;

          // If it is the current lambda, then do not asign it
          if (j == (I[l] - 1)) continue;

          INTEGER(id)[length(id) - (G[n+I[l]-1] - 1) + ++m] = j+1;
          REAL(lambda)[length(id) - (G[n+I[l]-1] - 1) + m] = val;
        }

        nreps+=1;
      }
    }

    if (nreps)
    {
      // Replacing elements from of the list (modified)
      SET_VECTOR_ELT(VECTOR_ELT(L1, i), 0, duplicate(id));
      SET_VECTOR_ELT(VECTOR_ELT(L1, i), 1, duplicate(lambda));
    }
    else {
      // Setting the list with the old elements
      SET_VECTOR_ELT(VECTOR_ELT(L1, i), 0,
        duplicate(VECTOR_ELT(VECTOR_ELT(L0,i),0)));
      SET_VECTOR_ELT(VECTOR_ELT(L1, i), 1,
        duplicate(VECTOR_ELT(VECTOR_ELT(L0,i),1)));
    }

    // Unprotecting elements
    UNPROTECT(4);
  }

  Rprintf("Exito\n") ;
  UNPROTECT(4);

  return L1;
}

Thanks again in advanced.

George Vega Yon
+56 9 7 647 2552
http://ggvega.cl

2013/11/5 George Vega Yon <g.vegayon at gmail.com>:

Thread (13 messages)

George Vega Yon Dynamic list creation (SEXP in C) returns error "unimplemented type (29) in 'duplicate'" Nov 4 Gabriel Becker Dynamic list creation (SEXP in C) returns error "unimplemented type (29) in 'duplicate'" Nov 5 Brian Ripley Dynamic list creation (SEXP in C) returns error "unimplemented type (29) in 'duplicate'" Nov 5 George Vega Yon Dynamic list creation (SEXP in C) returns error "unimplemented type (29) in 'duplicate'" Nov 5 Gabriel Becker Dynamic list creation (SEXP in C) returns error "unimplemented type (29) in 'duplicate'" Nov 5 George Vega Yon Dynamic list creation (SEXP in C) returns error "unimplemented type (29) in 'duplicate'" Nov 5 George Vega Yon Dynamic list creation (SEXP in C) returns error "unimplemented type (29) in 'duplicate'" Nov 5 George Vega Yon Dynamic list creation (SEXP in C) returns error "unimplemented type (29) in 'duplicate'" Nov 7 Romain Francois Dynamic list creation (SEXP in C) returns error "unimplemented type (29) in 'duplicate'" Nov 7 George Vega Yon Dynamic list creation (SEXP in C) returns error "unimplemented type (29) in 'duplicate'" Nov 7 Romain Francois Dynamic list creation (SEXP in C) returns error "unimplemented type (29) in 'duplicate'" Nov 7 Romain Francois Dynamic list creation (SEXP in C) returns error "unimplemented type (29) in 'duplicate'" Nov 7 George Vega Yon Dynamic list creation (SEXP in C) returns error "unimplemented type (29) in 'duplicate'" Nov 7