[R] Help with manipulation of matrix object
Gerrit Eichner
Gerrit.Eichner at math.uni-giessen.de
Fri Dec 7 08:41:59 CET 2012
Hi, Jeremy!
> ... I have a matrix, measuring m X n. An example matrix will be this:
> ... [snip] ...
> What I want to do is actually statistical manipulation of this matrix.
> To be more precise, I want to calculate the probability of each value,
> based on the assumption of a normal distribution, based on the row mean
> and standard deviation.
Hm, I think you have to be more precise since the probability to observe a
specific value under the assumption of a continuous distribution (e.g.,
the normal distribution) is zero -- always.
And you presumably do not want the first column to be included in the
calculation of the median, the sd, and finally the probability, do you?
However, if I understand correctly, you could maybe proceed as follows:
With
> row.median <- apply( sample[, -1], 1, median)
> row.sd <- apply( sample[, -1], 1, sd)
you could standardize the rows of your matrix by
> rowwise.standardized.sample <- (sample[, -1] - row.median) / row.sd
(utilizing the way matrices and vectors are handled in arithmetic
expression), and then use relations within the family of normal
distributions to compute density or cumulative probability values by
> dnorm( rowwise.standardized.sample) / row.sd
> pnorm( rowwise.standardized.sample)
Hth -- Gerrit
> For example,
>> sample[2,2] = 4.37914 <- This value is derived from the matrix, row 2
> column 2.
> To calculate the median (which i am using in place of the mean in the
> normal distribution), I know I have to use the apply function;
>
>
> the same goes for the standard deviation:
>
>> sd<-apply(sample,1, sd)
>
> With both the median and the sd in hand, I want to write the probability of
> observing the value reported at sample[2,2]
>
> I can do this manually, but as the dataframe gets larger and larger, this
> becomes more time consuming; and furthermore, I wish to write the
> probability as a matrix of the exact same dimensions, except that instead
> of a matrix reporting the values I now have a matrix of reporting the
> probabilities.
>
> The median and sd is calculated for each row.
>
> Any help/ thoughts on this will be greatly appreciated!
>
> Jeremy
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at 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.
>
>
More information about the R-help
mailing list