2017-07-14 59 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