Finding mean of standard normal distribution in a given interval

mean, normal-distribution, r

Solution

The problem is that the integral of dnorm(x) in the interval (-Inf to 0) isn't 1, that's why you got the wrong answer. To correct you must divide the result you got by 0.5 (the integral result). Like:

func <- function(x, ...) x * dnorm(x, ...)
integrate(func, -Inf, 0, mean=0, sd=1)$value / (pnorm(0, mean=0, sd=1) - pnorm(-Inf, mean=0, sd=1)) 

Adapt it to differents intervals should be easy.

Problem

I want to find mean of standard normal distribution in a given interval. For example, if I divide standard normal distribution into two ([-Inf:0] [0:Inf]) I want to get the mean of each half. Following code does almost exactly what I want: ``` divide <- 2 boundaries <- qnorm(seq(0,1,length.out=divide+1)) t <- sort(rnorm(100000)) means.1 <- rep(NA,divide) for (i in 1:divide) { means.1[i] <- mean(t[(t>boundaries[i])&(t<boundaries[i+1])]) } ``` But I need a more precise (and elegant) method to calculate these numbers (means.1). I tried the following code but it did not work (maybe because of the lack of my probability knowledge). ``` divide <- 2 boundaries <- qnorm(seq(0,1,length.out=divide+1)) means.2 <- rep(NA,divide) f <- function(x) {x*dnorm(x)} for (i in 1:divide) { means.2[i] <- integrate(f,lower=boundaries[i],upper=boundaries[i+1])$value } ``` Any ideas? Thanks in advance.

Original source