简体   繁体   中英

Environments in R functions and Rcpp

I am having an odd problem where a function I wrote changes the value of my input after it runs.

Here is my R code:

library(entropy)
y = c(4, 2, 3, 0, 2, 4, 0, 0, 2, 1, 1)
y=rbind(y,2*(y%%2),y%%3)
y
#4    2    3    0    2    4    0    0    2     1     1
#0    0    2    0    0    0    0    0    0     2     2
#1    2    0    0    2    1    0    0    2     1     1
freqs.shrink(y)

freqs.shrinkC<-function(y,lambda.freqs,verbose=TRUE) {
  if (missing(lambda.freqs)) {
    lambda.freqs = getlambdashrinkC(y)
  }
  if (verbose==TRUE) {
    cat(paste("Specified shrinkage intensity lambda.freq (frequencies):",
              round(lambda.freqs, 4)), "\n")
  }
  ismatrix<-attributes(y)$dim
  out<-freqsshrinkC(y,lambda.freqs)
  attr(out,"lambda.freq")=lambda.freqs
  attr(out,"dim")=ismatrix
  return(out)
}

freqs.shrinkC(y)
y
    #0.05280131 0.0374932 0.04514725 0.0221851 0.0374932 0.05280131 0.0221851 0.0221851 0.0374932 0.02983915 0.02983915
  #0.02218510 0.0221851 0.03749320 0.0221851 0.0221851 0.02218510 0.0221851 0.0221851 0.0221851 0.03749320 0.03749320
  #0.02983915 0.0374932 0.02218510 0.0221851 0.0374932 0.02983915 0.0221851 0.0221851 0.0374932 0.02983915 0.02983915

There is no reason that the value of y should change after running freqs.shrinkC since the function operates in its own environment I believe.

Here is my C++ code:

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
NumericVector freqsshrinkC(NumericVector y,double lambda) {
  int m=y.length();
  double n=0;
  for (int i=0;i<m;i++) {
    n+=y(i);
  }
  y=y/n;

NumericVector add(m,lambda/m);
   y=y*(1-lambda);

  y+=add;
  return y;
}

// [[Rcpp::export]]
double getlambdashrinkC(NumericVector y) {
  double n=0;
  int m=y.length();
  double lambda;
  for (int i=0;i<m;i++) {
    n+=y[i];
  }
  NumericVector u=y/n;


  NumericVector temp(m,1.0);
  NumericVector varu=u*(temp-u)/(n-1);

  double msp=0;
  for (int i=0;i<m;i++) {
    msp+=pow(u[i]-(1.0/m),2);
  }
  if (msp==0) {
    lambda=1;
  } else {
    lambda=0;
    for (int i=0;i<m;i++) {
    lambda+=varu[i];
    }
    lambda=lambda/msp;
  }
  if (lambda>1) {
    lambda=1;
  }
  if (lambda<0) {
    lambda=0;
  }
  return lambda;
}

I am new to C++ and Rcpp so I apologize if my code is inelegant. If anyone is curious, I am rewriting the entropy package using Rcpp as an exercise. I'm stumped on why y is changing values when I run my function so any help is appreciated.

Regards,

Carl

Most likely it has to do with the fact that your freqsshrinkC function is doing (modifying) operations on its y argument directly. Since Rcpp::Vector s are proxy objects, this will affect the original object you pass in. Try using Rcpp::clone to make a deep copy of the vector you pass in, as below:

// [[Rcpp::export]]
NumericVector freqsshrinkC2(NumericVector y_, double lambda) {
  Rcpp::NumericVector y = Rcpp::clone(y_);
  int m = y.length();
  double n = 0;
  for (int i = 0; i < m; i++) {
    n += y(i);
  }
  y = y/n;

  NumericVector add(m,lambda/m);
  y = y*(1-lambda);

  y += add;
  return y;
}

/*** R

y1 <- c(4, 2, 3, 0, 2, 4, 0, 0, 2, 1, 1)
y1 <- rbind(y1, 2*(y1%%2), y1%%3)
x1 <- freqsshrinkC(y1, 1.5)

y2 <- c(4, 2, 3, 0, 2, 4, 0, 0, 2, 1, 1)
y2 <- rbind(y2, 2*(y2%%2), y2%%3)
x2 <- freqsshrinkC2(y2, 1.5)


all.equal(y1, x1)
R> all.equal(y1, x1)
#[1] TRUE       # y1 was modified

all.equal(y2, x2)
R> all.equal(y2, x2)
#[1] "Mean relative difference: 1.01039"  # y2 was not

*/

Where freqsshrinkC is the version in your question, and freqsshrinkC2 uses Rcpp::clone on the (now renamed to y_ ) input vector.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM