Elizabeth
Elizabeth

Reputation: 241

R use propagate() to calculate the propagated error for summing up a column

I am learning the propagate function and want to use it to calculate the propagated error of summing the raw data in a data frame column. Each row in the data frame is a record, provided with raw data and standard error. A total of one hundred rows (records) are in the data frame. I tried the below code, but it does not work. Can anyone help please!

library(propagate)

set.seed(1)    
#create data frame with 100 records with random mean and se
df <- data.frame(raw.data= runif(n=100, min=20, max=100),
                 se = runif(n=100, min=1, max=10))
# set record name, each record is one term in the equation
for (i in 1:100){
  rownames(df)[i]<- paste("x",i, sep="")
}
#transpose to the format required in propagate()
# first row is mean, second row is se
df<-t(df) 

#get the equation
col <- paste (colnames(df), collapse="+") 
EXPR <- expression(col) # so it should be x1+x2+x3+...+x100
RES1 <- propagate(expr = EXPR, data = df, type = "stat", 
                  do.sim = TRUE, verbose = TRUE)

error message "Error in propagate(expr = EXPR, data = df, type = "stat", do.sim = TRUE, : propagate: variable names of input dataframe and expression do not match!"

Upvotes: 1

Views: 252

Answers (2)

Kalman Toth
Kalman Toth

Reputation: 1

The issue with your code is that the expression() function does not work the way you try to use it. Try:

EXPR <- parse(text = col)

then rerun your code and you should obtain:

RES1
Results from uncertainty propagation:
    Mean.1     Mean.2       sd.1       sd.2       2.5%      97.5% 
6142.77652 6142.77652   61.58669   61.58669 6022.06882 6263.48422 
Results from Monte Carlo simulation:
      Mean         sd     Median        MAD       2.5%      97.5% 
6142.78635   61.59167 6142.86039   61.61596 6022.07778 6263.53591 

Upvotes: 0

Carl Witthoft
Carl Witthoft

Reputation: 21532

Not familiar with this package, but looking at the documentation, I suspect that your main problem is that the expr argument needs variables as inputs, but you are feeding it column names.

The following produces a result...

Rgames> x <- df[1,]
Rgames> y <-df[2,]

Rgames> df1 <- cbind(x,y)
Rgames> RES1 <- propagate(expr = expression(x/y), data = df1, type = "stat",do.sim = TRUE, verbose = TRUE)
Rgames> RES1
Results from uncertainty propagation:
   Mean.1    Mean.2      sd.1      sd.2      2.5%     97.5% 
10.689918 13.292814  6.926797  8.087914 -2.559210 29.144837 
Results from Monte Carlo simulation:
     Mean        sd    Median       MAD      2.5%     97.5% 
16.153167 15.244058 11.304293  8.550236  2.433047 61.741723 

Upvotes: 0

Related Questions