0

I'm new to R and don't have much background in statistics, but my graduate study is related to statistics and I'm currently working on constructing an index (similar to constructing standardised precipitation index) and would require fitting a cumulative density function into inverse normal distribution. Seems that R doesn't have built-in inverse normal distribution function. Is there a way to do it?

Here attached is my code. Thanks!

timeScale <- 3
SNOmm_1_3mon <- read.delim('SNOmm_1.txt')
nVal <- nrow(SNOmm_1_3mon)
nYear <- nVal/12
totWindows <- nVal - timeScale +1
SWEI <- matrix(0,totWindows,1)
nWindow <-0
Q <- c(0,0,0,0,0,0,0,0,0,0,0,0)
"fit Pearson Type III distribution and retain its parameters"

for(k in 1:12){
  endMonth <- timeScale+k-1
  if (endMonth>12){nYear2 <- nYear-1
  } else {nYear2 <- nYear
  }
  precipCum <- list()
  for(i in 1:nYear2){
    nWindow<-nWindow+1
    sumVal<-0
    for(j in 1:timeScale){
      sumVal <- sumVal+SNOmm_1_3mon[(i-1)*12+endMonth-j+1,1]
    }
    precipCum[i] <- sumVal
  }
  precipCumData <- as.numeric(as.character(unlist(precipCum)))
  LMOMENT <- samlmu(precipCumData)
  PE3PARA <- pelpe3(LMOMENT)
  
}

"Calcualte SWEI"

startMonth <- 1
for(i in 1:(nVal-timeScale+1)){
  if(i<-1){kk<-timeScale
  } else {kk<-timeScale+i-1
  }
  sumVal2 <- 0
  for(j in 1:timeScale){
    sumVal2 <- sumVal2+SNOmm_1_3mon[kk-j+1,1]
  }
  if(sumVal2>0){Xn_cdf<-cdfpe3(sumVal2,PE3PARA)
  Cum_prob<-Q[startMonth]+(1-Q[startMonth])*Xn_cdf
  } else {Cum_prob <- Q[startMonth]
  }
  **Z<-norminv(Cum_prob)
  SWEI[i]<-Z
  startMonth <- startMonth+1
  if(startMonth>13){startMonth <- 1
  }
  sumVal3[i] <- sumVal2**

}

The "Z<- norminv part doesn't exist in Rstudio, it's just a way for me to represent what I'd like to do in that part. Thanks!

Phil
  • 7,287
  • 3
  • 36
  • 66
  • 2
    From here: https://influentialpoints.com/notes/n3rnorm.htm "R's qnorm function calculates which value in a normal population (y) has a given proportion (pN) of values below it. In other words it does the inverse of the cumulative normal function." – Phil Dec 01 '20 at 18:13
  • Also, check out [this](https://stackoverflow.com/questions/19589191/the-reverse-inverse-of-the-normal-distribution-function-in-r). – eastclintw00d Dec 01 '20 at 20:51

0 Answers0