0

I wrote a function to calculate PAFPM2.5

This function works fine.

However, there is a fatal error in my last step where I try to apply the function to a set of vector values. In the first line of the code, I manually put values and there the code works correctly. But I don't need 1 value, but i need PAFPM2.5 for hundreds of value, whereby RR.low, RR.up and RR.est are read from a vector as you can see in the 2nd line of the code.

Unfortuantely, This results in an error : "Error in PAF.summary(low = sectors$RR.low, up = sectors$RR.up, est = sectors$RR.est) : unused arguments (low = sectors$RR.low, up = sectors$RR.up, est = sectors$RR.est)"

For clarity, sectors$RR.low contains around 100 RR.low values and the same is true for the other columns.

PAFPM2.5<-PAF.summary(RR.low = 1.4, RR.up = 1.6, RR.est=1.5) #PM2.5

PAFPM2.5<-PAF.summary(low = sectors$RR.low, up = sectors$RR.up, est=sectors$RR.est) #PM2.5

excel file

##full script
library(readxl)
library(triangle)

sectors  <- read_excel("sectors.xlsx")  ##of handmatig inlezen via de knop (import dataset)
RR.low <- numeric()
RR.up <- numeric()
RR.est <- numeric()

RR.low <- sectors$RR.low
RR.up <- sectors$RR.up
RR.est <- sectors$RR.est


PAF.summary<-function(RR.low,RR.up,RR.est){
  
  ### RR.est is the point estimate of the RR
  ### (RR.low,RR.up) is the CI of the RR
  
  # relative risk
  r <- rtriangle(10000,a=RR.low,b=RR.up,c=RR.est)
  
  # proportion of population with risk factor
  p<-1.0  #in a statistical sector, 100% exposed to mean concentration within sector 
  
  # traditional PAF method
  PAF<-(p*(r-1))/(p*(r-1)+p)
  
  return(quantile(PAF,c(0.025,0.5,0.975)))
  
}


PAFPM2.5<-PAF.summary(RR.low = 1.4, RR.up = 1.6, RR.est=1.5)  #PM2.5  ###works fine

##should be converted to store many values based on the sectors file 
PAFPM2.5<-PAF.summary(RR.low = sectors$RR.low, RR.up = sectors$RR.up, RR.est=sectors$RR.est)  #PM2.5
Andre Wildberg
  • 12,344
  • 3
  • 12
  • 29
bram
  • 47
  • 6
  • 1
    What is the output of ```PAF.summary```? Is it a value, list, dataframe? – one Jan 24 '23 at 15:30
  • 1
    Please include the libraries you're using. – Andre Wildberg Jan 24 '23 at 15:32
  • 2.5% : 0.296 - 50% 0.333 - 97.5% : 0.366 for example - it calculates the confidence interval and a mean estimate. Libraries: "triangle", "readxl" – bram Jan 24 '23 at 15:32
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Jan 24 '23 at 15:35
  • 1
    I added the excel source file and the entire script now. It should be entirely reproducible. – bram Jan 24 '23 at 15:40

1 Answers1

0

One way is to vectorized the function:

PAF.summary<-function(RR.low,RR.up,RR.est,id=NA){
  
  ### RR.est is the point estimate of the RR
  ### (RR.low,RR.up) is the CI of the RR
  
  library(triangle)
  
  # relative risk
  rtriangle2 <- Vectorize(rtriangle)
  r <- rtriangle2(10000,a=RR.low,b=RR.up,c=RR.est)
  
  # proportion of population with risk factor
  p<-1.0  #in a statistical sector, 100% exposed to mean concentration within sector 
  
  # traditional PAF method
  PAF<-(p*(r-1))/(p*(r-1)+p)
  output <- as.data.frame(t(apply(PAF,2,quantile,c(0.025,0.5,0.975))))
  if(!all(is.na(id))){
    output <- cbind(id,output)
  }
  return(output)
  

Example:

RR.low <- c(1.26,1.19)
RR.est <- c(1.34,1.25)
RR.up <- c(1.42,1.38)
SECTOR <- c("A575","A576")

PAF.summary(RR.low = 1.4, RR.up = 1.6, RR.est=1.5)
       2.5%       50%     97.5%
1 0.2970301 0.3332704 0.3665127


PAF.summary(RR.low, RR.up, RR.est,SECTOR)
    id      2.5%       50%     97.5%
1 A575 0.2175932 0.2538054 0.2865436
2 A576 0.1712424 0.2119270 0.2623992
one
  • 3,121
  • 1
  • 4
  • 24