Firas Swidan <frsswdn <at> gmail.com> writes:

> 
> Hi,
> 
> I have two (different types of) measurements, say X and Y, resulting from
> the same set of experiments. So X and Y are paired: (x_1, y_1), (x_2, y_2),
> ...
> 
> I am trying to calculate the standard deviation of Y with respect to X. In
> other words, in terms of the scatter plot of X and Y, I would like to divide
> it into bins along the X-axis and for each bin calculate the standard
> deviation along the Y results in that bin. (Though I am not totally sure,
> this seems to remind me of the conditional expectation of Y given X - maybe
> it is called the conditional deviation?)
> 
> Is their a built in procedure in R for calculating the above? Otherwise,
> what would be the easiest way to achieve it? (factors maybe?)
> 
> Thankful for the help,
> Firas.
> 

Something like the following should give you what you want:

> x <- rnorm(50)
> y <- rnorm(50)
> tapply(y, cut(x, 10, include.lowest=TRUE), sd)
 [-2.19,-1.75]   (-1.75,-1.3]   (-1.3,-0.86] (-0.86,-0.415] (-0.415,0.029] 
     0.7569111      0.1671267      0.5620591      1.1280510      0.7772356 
 (0.029,0.473]  (0.473,0.918]   (0.918,1.36]    (1.36,1.81]    (1.81,2.25] 
     0.5600363      0.7681090      0.9754286      0.3184307      0.2410181

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to