r - Subtracting columns in a matrix created by user-defined function -


i'm having trouble subtracting 2 columns generated user-defined function in r.

my data:

dat <- data.frame(matrix(rnorm(1000), nrow=50))   

my function:

ci <- function(int, dat){   alpha = (1-int) / 2   z <- qnorm(1-alpha)   n <- sum(is.na(dat)==false)   av <- colmeans(dat, na.rm=true)                   me <- z*(n/sqrt(n))   lower <- av - me   upper <- av + me    return(cbind(lower, upper)) } 

the function trying manually determine upper , lower part of confidence interval column of values (part of bigger scheme). want subtract upper lower ci range. attemp:

ci(int=0.99, dat=dat) cidat <- ci(int=0.99, dat=dat) <-as.numeric(cidat[,2]) low <-as.numeric(cidat[,1]) up+low up-low 

the + low works, - low gives uniform result:

> up+low      [1] -0.24418497  0.26253152 -0.35225948  0.27564574 -0.10129327 -0.14313671  0.11122208  0.30469330 -0.09498985     [10] -0.05658091 -0.19336386 -0.31854479 -0.05433397 -0.03539858  0.07648641 -0.46213684  0.66736452  0.42824967     [19]  0.29344554 -0.06257925 > up-low      [1] 162.9097 162.9097 162.9097 162.9097 162.9097 162.9097 162.9097 162.9097 162.9097 162.9097 162.9097 162.9097     [13] 162.9097 162.9097 162.9097 162.9097 162.9097 162.9097 162.9097 162.9097 

am missing obvious?

have @ up , low. values far large standard-normal distribution.

mean(up)      mean(low) [1] 81.47071  [1] -81.43904 

you have thinking error somewhere in ci() function. if understand correctly, have multidimensional standard-normal distribution dat , want span of confidence interval each dimension (column) of dat.

i suggest doing follows:

ci_span <- function(int, dat) {      alpha <- (1 - int) / 2     p_range <- c(alpha, 1 - alpha)     apply(dat, 2, function(icoldat) as.numeric(diff(quantile(icoldat, probs = p_range))))  } 

this returns spans of each columns ci. r has built in function computing empirical quantiles called quantile.

hth, d


Comments

Popular posts from this blog

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -

python 3.x - Mapping specific letters onto a list of words -