quantile

R quantile by groups with assignments

自古美人都是妖i 提交于 2020-02-04 01:23:40
问题 I have the following df: group = rep(seq(1,3),30) variable = runif(90, 5.0, 7.5) df = data.frame(group,variable) I need to i) Define quantile by groups, ii) Assign each person to her quantile with respect to her group. Thus, the output would look like: id group variable quantile_with_respect_to_the_group 1 1 6.430002 1 2 2 6.198008 3 ....... There is a complicated way to do it with loops and cut function over each groups but it is not efficient at all. Does someone know a better solution ?

Get quantile for each value

我的未来我决定 提交于 2020-01-24 17:40:11
问题 Is there an implemented (!) function in R which gives you the empirical quantile for each value? I couldn't find any ... Let's say we have x x = c(1,3,4,2) I want to have the quantile of each element. [1] 0.25, 0.75, 1, 0.5 Thank you very much! 回答1: You can use the ecdf() function: ecdf(x)(x) [1] 0.25 0.75 1.00 0.50 ecdf(x) creates a function, and you pass the elements of x to that function. The syntax admittedly looks strange 来源: https://stackoverflow.com/questions/50842640/get-quantile-for

Plot quantiles of distribution in ggplot2 with facets

有些话、适合烂在心里 提交于 2020-01-23 12:46:11
问题 I'm currently plotting a number of different distributions of first differences from a number of regression models in ggplot. To facilitate interpretation of the differences, I want to mark the 2.5% and the 97.5% percentile of each distribution. Since I will be doing quite a few plots, and because the data is grouped in two dimension (model and type), I would like to define and plot the respective percentiles in the ggplot environment. Plotting the distributions using facets gets me to

Calculation of return levels based on a GPD in different R packages

梦想的初衷 提交于 2020-01-23 08:30:15
问题 I am performing an extreme value analysis for meteorological data, to be precise for precipitation data available in mm/d. I am using a threshold excess approach for estimating the parameters of a generalized Pareto distribution with a maximum likelihood method. The aim is to calculate several return levels (i.e. the 2, 5, 10, 20, 50, 100 year event) for daily precipitation. While the R code works fine, I am wondering why I get clearly different results when calculating return levels based on

Compute quantiles incorporating Sample Design (Survey package)

不羁岁月 提交于 2020-01-21 10:35:17
问题 I want to compute a new column using the quantiles of another column (a continuous variable) incorporating the Sample Design of a complex survey. The idea is to create in the the data frame a new variable that indicates which quantile group each observation falls into Here is how I execute the idea without incorporating the sample design, so you can understand what I'm aiming for. # Load Data data(api) # Convert data to data.table format (mostly to increase speed of the process) apiclus1 <-

Put returns into quantiles for many time series

点点圈 提交于 2020-01-05 21:11:12
问题 I have a xts object of monthly returns (one column is a time series for one instrument). I want to know the quantile for each return, each month. I have my own set of instruments prices from a local database but I can reproduce with getSymbols . I used quantile on stock returns to get the boundaries of my quantile. Then I tried to use cut to divide my returns into quantile but I am stuck there. Ideally I should have a time series of monthly quantile for each instrument. require(quantmod)

geom_quantile full range in ggplot2

女生的网名这么多〃 提交于 2020-01-03 19:16:24
问题 Is there a way how to set full_range = T parametr somehow in ggplot ? library(ggplot2) ggplot(mtcars, aes(hp, disp)) + geom_point() + #geom_smooth(method = "lm", aes(group = factor(gear), color = factor(gear)), fullrange = T) geom_quantile(quantiles = 0.5, aes(group = factor(gear), colour = factor(gear)), fullrange = T) So the quantile regression line would be "as long" as when using geom_smooth above? Is there a way how to make it work? Also is there a way how to plot full range when using

geom_quantile full range in ggplot2

坚强是说给别人听的谎言 提交于 2020-01-03 19:16:11
问题 Is there a way how to set full_range = T parametr somehow in ggplot ? library(ggplot2) ggplot(mtcars, aes(hp, disp)) + geom_point() + #geom_smooth(method = "lm", aes(group = factor(gear), color = factor(gear)), fullrange = T) geom_quantile(quantiles = 0.5, aes(group = factor(gear), colour = factor(gear)), fullrange = T) So the quantile regression line would be "as long" as when using geom_smooth above? Is there a way how to make it work? Also is there a way how to plot full range when using

quantile vs ecdf results

三世轮回 提交于 2020-01-03 18:46:49
问题 I am trying to use ecdf, but I am not sure if I am doing it right. My ultimate purpose is to find what quantile corresponds to a specific value. As an example: sample_set <- c(20, 40, 60, 80, 100) # Now I want to get the 0.75 quantile: quantile(x = sample_set, probs = 0.75) #result: 75% 80 # Let's use ecdf ecdf(x = sample_set) (80) #result 0.8 Why is there this discrepancy? Am I doing some trivial mistake, or it depends on the way quantile makes its calculations? Thanks, Max 回答1: There are

d3 quantile scale force first quantile

雨燕双飞 提交于 2019-12-25 06:44:27
问题 I'm building a heat map with a color gradient from green to red. I want cells with value 0 to be green, and values greater or equal than 1 to take the other colors. I'm building the scale this way : var colors = [ '#27C24C', '#7DB22E', '#D4A10F', '#F97C20', '#F35F40', '#FF0000' ]; var colorScale = d3.scale.quantile() .domain([0, d3.max(data, function (d) { return d.value; })]) .range(colors); But this returns me the following quantiles : [239.16666666666677, 478.3333333333332, 717.5, 956