full conditions! [R for BE/BA]

posted by mittyri – Russia, 2019-04-26 16:20 (523 d 14:28 ago) – Posting: # 20245
Views: 4,392

Dear Helmut,

the code is worth a thousand sentences
loc.stat <- function(x, type, na.rm, distr) {
  non.numerics    <- which(is.na(suppressWarnings(as.numeric(x))))
  x[non.numerics] <- NA
  x <- as.numeric(x)
  if (distr == "nonpar") {
    switch (type,
            pct2.5  = as.numeric(quantile(x, probs=0.025, na.rm=na.rm)),
            pct25   = as.numeric(quantile(x, probs=0.25, na.rm=na.rm)),
            median  = as.numeric(median(x, na.rm=na.rm)),
            pct75   = as.numeric(quantile(x, probs=0.75, na.rm=na.rm)),
            pct97.5 = as.numeric(quantile(x, probs=0.975, na.rm=na.rm)))
  } else if (distr == "normal") {
    mean.x <-mean(x, na.rm=na.rm)
    sd.x   <- sd(x, na.rm=na.rm)
    n.not.nas <- length(x)-length(which(is.na(x)))
    se <- sd.x/sqrt(n.not.nas)
    switch (type,
            cla2.5  = mean.x+qt(0.025, df=n.not.nas-1, lower.tail=TRUE)*se,
            ar.mean = mean.x,
            cla97.5 = mean.x+qt(0.975, df=n.not.nas-1, lower.tail=TRUE)*se)
  } else {
    NULL
  }
}
set.seed(123456)
x <- rnorm(12, 100, 20)
median <- loc.stat(x, "median", TRUE, "nonpar")
mean   <- loc.stat(x, "ar.mean", TRUE, "normal")

Kind regards,
Mittyri

Complete thread:

Activity
 Admin contact
21,090 posts in 4,398 threads, 1,469 registered users;
online 9 (0 registered, 9 guests [including 5 identified bots]).
Forum time: Thursday 06:48 CEST (Europe/Vienna)

In these days, a man who says a thing cannot be done
is quite apt to be interrupted by some idiot doing it.    Elbert Green Hubbard

The Bioequivalence and Bioavailability Forum is hosted by
BEBAC Ing. Helmut Schütz
HTML5