简体   繁体   中英

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:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM