Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Function for confidence intervals error code {

together with you, I have recently made the following function (the content is not important right now). It seems to be correct but when I try to process it, the following error turns up: Error: unexpected ‘}’ in " }". Do you know what I´ve made wrong?

Here´s the function, thank you in advance (btw I have checked every bracket):

Edit: Now it works:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

CI <- function(x, s, z, Fall) {

if (Fall == "Fall1") {
result <- mean(x) + c(-1,1)* qnorm(1-z/2)*(s/sqrt(length(x)))


} else if (Fall == "Fall2") {
result <- mean(x) + c(-1,1)* qt(p=1-a/2, df=length(x)-     1)*(sd(x)/sqrt(length(x)))

} else if (Fall == "Fall3") {
result <-mean(x)+c(-1,1)qnorm(1-z/2(s/sqrt(length(x))))

} else if (Fall == "Fall4"){
result <- mean(x)+c(-1,1)qt(p=1-a/2, df=length(x)-1)(sd(x)/sqrt(length(x)))

} else {result<-NA}

return(result)
}

CI(x=x, s=15, z=0.05, Fall="Fall1")

>Solution :

There are couple of errors – 1) else would not have a condition check, instead use else if, 2), the values to compare should be quoted "Fall1"

CI <- function(x, mean, sd, z, Fall)
  {
  if (Fall == "Fall1") {
    result <- mean(x) + c(-1, 1) * qnorm(1 - z / 2) * (sd / sqrt(length(x)))
    
    
  } else if (Fall == "Fall2") {
    result <-
      mean(x) + c(-1, 1) * qt(p = 1 - a / 2, df = length(x) - 1) * (sd(x) / sqrt(length(x)))
    
    
  } else if (Fall == "Fall3") {
    result <- mean(x) + c(-1, 1) * qnorm(1 - z / 2 * 
  (sd / sqrt(length(x))))
    
    
  } else if (Fall == "Fall4") {
    result <-
      mean(x) + c(-1, 1) * qt(p = 1 - a / 2, df = length(x) - 1) * (sd(x) / sqrt(length(x)))
  }
  else {
    result <- NA_real_
  }
  
  
  return(result)
  
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading