2017-07-14 50 views
0

我制作了quantilescut2函数大小相同,现在我想通过4个分位数制作4个不同的子集。通过分位数制作子集

第一和第四位数,我可以与该亚群功能使:

quantile1 <- subset (trial, NAG <22.1) 

quantile4 <- subset(trial, NAG >=61.6) 

但是,如果我试图让第二和第三位数的子集,它完全不是那么回事,我不明白为什么。这是我已经试过:

quantile2<- subset(trial, NAG >=22.1 | NAG<36.8) 

quantile3<-subset(trial, NAG >=36.8 | NAG <61.6) 

如果我用这个功能,R使得一个子集,但该子集是由观测的总数,这不可能是正确的。任何人有关语法错误的想法是或如何解决它?

在此先感谢!

+0

不应该是quantile3 <-subset(trial,NAG> = 36.8&NAG <61.6) – maller

+0

你是绝对正确的!它现在有效。有时它可以是如此简单..;) – Sjurliee

+0

查看[Venn-Diagrams](https://en.wikipedia.org/wiki/Venn_diagram)! – maller

回答

0

前段时间我有同样的问题(here)。我做了一个GetQuantile功能,可帮助您:

GetQuantile<-function(x,q,n){ 
    # Extract the nth quantile from a time series 
    # 
    # args: 
    # x = xts object 
    # q = quantile of xts object 
    # n = nthe quantile to extract 
    # 
    # Returns: 
    # Returns an xts object of quantiles 

    # TRUE/FALSE depending on the quantile we are looking for 
    if(n==1) # first quantile 
    test<-xts((coredata(x[,])<c(coredata(q[,2]))),order.by = index(x)) 
    else if (n== dim(q)[2]-1) # last quantile 
    test<-xts((coredata(x[,])>=c(coredata(q[,n]))),order.by = index(x)) 
    else # else 
    test<-xts( (coredata(monthly.returns[,])>=c(coredata(q[,n]))) & 
       (coredata(monthly.returns[,])<c(coredata(q[,(n+1)]))) ,order.by = index(x)) 
    # replace NA by FALSE 
    test[is.na(test)]<-FALSE 
    # we only keep returns for which we need the quantile 
    x[test==FALSE]<-NA 
    return(x) 
} 

使用此功能,我可以有我想要的位数的所有月度回报和NA其他地方的XTS。有了这个XTS我可以做一些东西,如计算平均每个分位数等..

monthly.returns.stock.Q1<-GetQuantile(stocks.returns,stocks.quantile,1) 
rowMeans(monthly.returns.stock.Q1,na.rm = TRUE) 
0

我有同样的问题。我用这个:

df$cumsum <- cumsum(df$var) 
# makes cumulative sum of variable; my data were in shares, so they added up 
# to 100 

df$quantile <- cut(df$cumsum, c(0, 25, 50, 75, 100, NA), names=TRUE) 
# cuts the cumulative sum at desired percentile 

对于没有股份来变量,我用从汇总,其中R为您提供位数的信息,然后根据这些值削减数据。

问:你的分位数是否相等?我的意思是,他们是否都包含25%的观察值?因为我的身材很差......也就是22%,28%等等。只是好奇你是怎么解决这个问题的。

+0

您可以使用'cut2'功能{Hmisc},而不是'cut'功能。 'cut2'将该组划分为相同大小的类别。 – Sjurliee

+0

感谢您的回答! – cremorna