Dear List, I have some discrete data and want to calculate the percentiles and the percentile ranks for each of the unique scores. I can calculate the percentiles with quantile(). I know that "ecdf" can be used to calculate the empirical cumulative distribution. However, I don't know how to exact the cumulative probabilities for each unique element. The requirement is similar to the "FREQUENCIES" in SPSS. Could someone help me in exacting the cumulative probabilities from the ecdf object? Thanks in advance! # Generating artificial data x <- round( rnorm(50, mean=50, sd=10) ) probs <- seq(0.1, 0.9, by=0.1) # Calculating percentiles x.percentiles <- quantile(x, probs) # Calculating percentile ranks x.ranks <- ecdf(x) Best, Mike
Dear Mike Mike Cheung writes: > Dear List, > > I have some discrete data and want to calculate the percentiles and the > percentile ranks for each of the unique scores. I can calculate the > percentiles with quantile(). > > I know that "ecdf" can be used to calculate the empirical cumulative > distribution. However, I don't know how to exact the cumulative > probabilities for each unique element. The requirement is similar to the > "FREQUENCIES" in SPSS. Could someone help me in exacting the cumulative > probabilities from the ecdf object? Thanks in advance! You can use the following function: f.freq <- function(x) { tab <- data.frame(table(x)) tab$Percent <- tab$Freq*100/length(x) tab$Cum.Percent[1] <- tab$Percent[1] for(i in 2:length(tab[,1])) tab$Cum.Percent[i] <- tab$Cum.Percent[i-1] + tab$Percent[i] tab } x <- round( rnorm(50, mean=50, sd=10) ) f.freq(x) This should give you a table analog to frequencies in SPSS. > > # Generating artificial data > x <- round( rnorm(50, mean=50, sd=10) ) > probs <- seq(0.1, 0.9, by=0.1) > # Calculating percentiles > x.percentiles <- quantile(x, probs) > # Calculating percentile ranks > x.ranks <- ecdf(x) > > Best, > Mike > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Best, Christoph -- Christoph Buser <buser at stat.math.ethz.ch> Seminar fuer Statistik, LEO C11 ETH (Federal Inst. Technology) 8092 Zurich SWITZERLAND phone: x-41-1-632-5414 fax: 632-1228 http://stat.ethz.ch/~buser/
Mike Cheung <mikewlcheung at hku.hk> writes:> I know that "ecdf" can be used to calculate the empirical cumulative > distribution. However, I don't know how to exact the cumulative > probabilities for each unique element. The requirement is similar to > the "FREQUENCIES" in SPSS. Could someone help me in exacting the > cumulative probabilities from the ecdf object? Thanks in advance! > > # Generating artificial data > x <- round( rnorm(50, mean=50, sd=10) ) > probs <- seq(0.1, 0.9, by=0.1) > # Calculating percentiles > x.percentiles <- quantile(x, probs) > # Calculating percentile ranks > x.ranks <- ecdf(x)I don't quite see why you call it "x.ranks", but it's just a function that you could evaluate at the step points, so continuing your code: val <- sort(unique(x)) cbind(val,cum.prop=x.ranks(val)) I'd go for a more direct approach though: cumsum(prop.table(table(x))) -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
Seemingly Similar Threads
- data vector to corresonding percentile ranks
- getting percentiles by factor
- Finding percentile of a value from an empirical distribution
- how to compute the inverse percentile of a given observation w.r.t. a reference distribution
- 2 functions with same name - what to do to get the one I want