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

49 views Asked by At

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!"

1

There are 1 answers

5
Carl Witthoft On

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