Home > Enterprise >  Function for confidence intervals error code {
Function for confidence intervals error code {

Time:05-31

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:

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

CodePudding user response:

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)
  
}
  • Related