[R] empirical df, cdf and crossing points

Michael Bedward michael.bedward at gmail.com
Fri Sep 24 07:50:18 CEST 2010


Hello,

Not sure about packages to suggest but some of this can be done with
base stats...

# Generate densities using common from, to and n args
male <- rnorm(100, 80, 10)
female <- rnorm(100, 60, 10)
male.d <- density(male, from=0, to=150, n=1024)
female.d <- density(female, from=0, to=150, n=1024)
plot(0, type="n", xlim=c(0, 150), xlab="weight", ylim=c(0,
max(c(male.d$y, female.d$y))), ylab="density")

# resist gender stereotypes
lines(male.d, col="pink")
lines(female.d, col="blue")

# Finding crossing points (only one in these toy data)
EPS <- 1.0e-5  # tune this for your data
pos <- male.d$y > EPS & female.d$y > EPS
abs.diff <- abs(male.d$y[pos] - female.d$y[pos])
cross <- abs.diff - min(abs.diff) < EPS

# Weights of crossing points
cross.weights <- male.d$x[pos][cross]

# Examine cdfs
male.cdf <- ecdf(male)
female.cdf <- ecdf(female)

plot(0, type="n", xlim=c(0, 150), xlab="weight", ylim=c(0, 1), ylab="prob")
lines(male.cdf)
lines(female.cdf)

# Comparison for values in vector w
higher <- ifelse(male.ecdf( w ) > female.ecdf( w ), "m", "f")

Hope this helps,
Michael


On 23 September 2010 23:39, anupam <anuptyagi at gmail.com> wrote:
> Hello, I am using survey data with two stage sampling for two sub-populations.
> I am looking for a package (or packages) that can do the following for a
> measure of weight. The sub-populations are M (male) and F (female).
>
> (1) empirical df and cdf for weight, and compare that across two sub-
> populations.
> (i) get the x,y values where the plotted curves cross (more than one x in my
> data),
> (2) test for dominance,
> (3) give x value, and know which of the csf curves first or second order
> dominate at that value (using area under the curve).
>
> How can I do this?
>
> ______________________________________________
> 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