我写了一个函数来计算向量中每行的十分位数.我这样做的目的是创建图形来评估预测模型的功效.必须有一种更简单的方法来做到这一点,但我暂时还没弄清楚.有没有人知道如何在没有这么多嵌套的ifelse()语句的情况下以这种方式对矢量进行评分?我包含了该函数以及一些代码来复制我的结果.
# function
decile <- function(x){
deciles <- vector(length=10)
for (i in seq(0.1,1,.1)){
deciles[i*10] <- quantile(x, i)
}
return (ifelse(x<deciles[1], 1,
ifelse(x<deciles[2], 2,
ifelse(x<deciles[3], 3,
ifelse(x<deciles[4], 4,
ifelse(x<deciles[5], 5,
ifelse(x<deciles[6], 6,
ifelse(x<deciles[7], 7,
ifelse(x<deciles[8], 8,
ifelse(x<deciles[9], 9, 10))))))))))
}
# check functionality
test.df <- data.frame(a = 1:10, b = rnorm(10, 0, 1))
test.df$deciles <- decile(test.df$b)
test.df
# order data frame
test.df[with(test.df, order(b)),]
Run Code Online (Sandbox Code Playgroud) 这是一个 data.frame,其第三个“列”实际上是一个矩阵:
pred.Alb <- structure(list(Age =
c(20, 30, 40, 50, 60, 70, 80, 20, 30, 40,
50, 60, 70, 80), Sex = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Male", "Female"),
class = "factor"),
pred = structure(c(4.34976914720261, 4.3165897157342, 4.2834102842658,
4.23952109360855, 4.15279286619591, 4.05535487959442, 3.95791689299294,
4.02417706540447, 4.05661037005163, 4.08904367469879, 4.0942071858864,
3.9902915232358, 3.85910606712565, 3.72792061101549, 4.37709246711838,
4.38914906337186, 4.40120565962535, 4.3964228776405, 4.32428258270227,
4.23530290952571, 4.14632323634915, 4.3, 4.3, 4.3, 4.28809523809524,
4.22857142857143, 4.15714285714286, 4.08571428571429, 4.59781730640631,
4.59910124381436, 4.60038518122242, 4.58132673532165, 4.48089875618564,
4.36012839374081, 4.23935803129598, …Run Code Online (Sandbox Code Playgroud) 有一个data.frame()我想要计算分位数的列:
tert <- c(0:3)/3
data <- dbGetQuery(dbCon, "SELECT * FROM tablename")
quans <- mapply(quantile, data, probs=tert, name=FALSE)
Run Code Online (Sandbox Code Playgroud)
但结果只包含分位数返回列表的最后一个元素,而不是整个结果.我也收到警告longer argument not a multiple of length of shorter.如何修改我的代码才能使其正常工作?
PS:单独的功能就像魅力一样,所以我可以使用for循环:
quans <- quantile(a$fileName, probs=tert, name=FALSE)
Run Code Online (Sandbox Code Playgroud)
PPS:什么也有效不指定probs
quans <- mapply(quantile, data, name=FALSE)
Run Code Online (Sandbox Code Playgroud) 我知道如何找到经验分布的分位数。
set.seed(1)
x = rnorm(100)
q = quantile(x, prob=seq(0,1,.01))
Run Code Online (Sandbox Code Playgroud)
是否有一个函数可以使我获得一些训练集所属的分位数仓?在这个例子中
R) x[1]
[1] -0.6264538107
R) q
0% 1% 2% 3% 4% 5% 6% 7% 8%
-2.214699887177 -1.991605177777 -1.808646490230 -1.532008555284 -1.472864960560 -1.381744198182 -1.282620249360 -1.255240516814 -1.226934277726
9% 10% 11% 12% 13% 14% 15% 16% 17%
-1.137935552774 -1.052657473293 -0.946201701058 -0.847444894718 -0.822439213796 -0.754080533415 -0.714945447616 -0.707887360796 -0.691941403160
18% 19% 20% 21% 22% 23% 24% 25% 26%
-0.637668149828 -0.622231094280 -0.613869230709 -0.594247090071 -0.576841631266 -0.569725969545 -0.548795719430 -0.494242549079 -0.474635485293
27% 28% 29% 30% 31% 32% 33% 34% 35%
-0.451421239288 …Run Code Online (Sandbox Code Playgroud) 我试图使用函数创建新变量,lapply而不是使用循环在数据中正常工作.我曾经使用Stata,并且会用类似于此处讨论的方法解决这个问题.
由于在R中以编程方式命名变量是如此困难或至少是尴尬(并且似乎你不能使用索引assign),我已经将命名过程留到了之后lapply.然后我使用for循环在合并之前进行重命名,然后再用于合并.有更有效的方法吗?我该如何更换循环?我应该做某种重塑吗?
#Reproducible data
data <- data.frame("custID" = c(1:10, 1:20),
"v1" = rep(c("A", "B"), c(10,20)),
"v2" = c(30:21, 20:19, 1:3, 20:6), stringsAsFactors = TRUE)
#Function to analyze customer distribution for each category (v1)
pf <- function(cat, df) {
df <- df[df$v1 == cat,]
df <- df[order(-df$v2),]
#Divide the customers into top percents
nr <- nrow(df)
p10 <- round(nr * .10, 0)
cat("Number of people in the Top 10% :", p10, …Run Code Online (Sandbox Code Playgroud) 我正在尝试更改箱线图的上限和下限.
我需要从5%变为10%(较低),从95%变为90%(较高).
而且,我必须使用标准的boxplot函数(不幸的是没有ggplot).
我试图计算分位数(quantile函数),但我不知道boxplot函数如何理解新值.
一些想法?
我需要创建一个 QQ 图来检查我观察到的数据是否符合泊松分布。
这是我的数据框:
df = read.table(text = 'Var1 Freq
1975 10
1976 12
1977 9
1978 14
1979 14
1980 11
1981 8
1982 7
1983 10
1984 8
1985 12
1986 9
1987 10
1988 9
1989 10
1990 9
1991 11
1992 12
1993 9
1994 10', header = TRUE)
Run Code Online (Sandbox Code Playgroud)
这df$Freq专栏是我感兴趣的专栏,因为观察结果代表了每年的事件数量。
我知道我必须使用该qqplot函数以及qpois创建理论分位数的函数,但是如何呢?
我试图将这些行从R翻译成Julia:
n <- 100
mean <- 0
sd <- 1
x <- qnorm(seq(1 / n, 1 - 1 / n, length.out = n), mean, sd)
Run Code Online (Sandbox Code Playgroud)
但是,我在使用qnorm函数时遇到了麻烦.我搜索了"分位数函数"并找到了quantile()函数.但是,R的版本返回长度为100的向量,而Julia的版本返回长度为5的向量.
这是我的尝试:
import Distributions
n = 100
x = Distributions.quantile(collect(range(1/n, stop=1-1/n, length=n)))
Run Code Online (Sandbox Code Playgroud) 我的理解是两种方式都应该给出与较低尾部概率相对应的分位数。但是,我得到了不同的结果。例如:-qgeom(0.99,0.5)在 R 中给出 6,而geom.ppf(0.99,0.5)在 Python 中给出 7。
我有以下数据框
item_id group price
0 1 A 10
1 3 A 30
2 4 A 40
3 6 A 60
4 2 B 20
5 5 B 50
Run Code Online (Sandbox Code Playgroud)
我希望根据每个组的价格添加一个分位数列,如下所示:
item_id group price quantile
01 A 10 0.25
03 A 30 0.5
04 A 40 0.75
06 A 60 1.0
02 B 20 0.5
05 B 50 1.0
Run Code Online (Sandbox Code Playgroud)
我可以遍历整个数据帧并为每个组执行计算。但是,我想知道有没有更优雅的方法来解决这个问题?谢谢!
好吧,所以我这里有一个最奇怪的问题.当我们用一个独立变量X的特定分位数划分空格时,我正在取一个因变量Y的均值.
我的问题是,R中的分位数函数没有返回我的自变量X范围内的值,但是当它打印到屏幕上时返回的值是正确的值.是什么让这个陌生人只会发生特定的分位数.
一些示例代码来演示这种奇怪的效果:
x<-c(1.49,rep(1.59,86))
quantile(x,0.05) # returns 1.59, the correct value
# However both of these return all values as false
table(x>=quantile(x,0.05))
table(x==quantile(x,0.05))
# But if we take a quantile at 0.075 it works correctly
table(x>=quantile(x,0.075))
Run Code Online (Sandbox Code Playgroud)
你们可以提供的任何见解将不胜感激.
有谁能告诉我R中的分位数功能和HMISC包中的cut2功能之间的区别?
我知道分位数有9种不同的方法来指定四分位数.但是,当我使用函数cut2(mydata,g = 4)时,输出的四分位数不对应于任何分位数函数输出.
任何帮助非常感谢.
提前致谢.
我想为每个日期创建一个分位数列。计算每个唯一值销售值的分位数。即类别始终对应于每个特定日期的相同销售数字。
\n\n我有按日期索引的数据框。有许多日期和多个相同的日期。1 天 df 的子集示例:
\n\n Category Sales Ratio 1 Ratio 2\n11/19/2016 Bar 300 0.46 0.96\n11/19/2016 Bar 300 0.56 0.78\n11/19/2016 Bar 300 0.43 0.96\n11/19/2016 Bar 300 0.47 0.94\n11/19/2016 Casino 550 0.92 0.12\n11/19/2016 Casino 550 0.43 0.74\n11/19/2016 Casino 550 0.98 0.65\n11/19/2016 Casino 550 0.76 0.67\n11/19/2016 Casino 550 0.79 0.80\n11/19/2016 Casino 550 0.90 0.91\n11/19/2016 Casino 550 0.89 0.31\n11/19/2016 Caf\xc3\xa9 700 0.69 0.99\n11/19/2016 Caf\xc3\xa9 700 0.07 0.18\n11/19/2016 Caf\xc3\xa9 700 0.75 0.59\n11/19/2016 Caf\xc3\xa9 700 0.07 0.64\n11/19/2016 Caf\xc3\xa9 700 0.14 0.42\n11/19/2016 Caf\xc3\xa9 700 0.30 …Run Code Online (Sandbox Code Playgroud) quantile ×13
r ×11
dataframe ×2
distribution ×2
pandas ×2
python ×2
boxplot ×1
ecdf ×1
group-by ×1
hmisc ×1
julia ×1
lattice ×1
percentile ×1
plot ×1
poisson ×1
python-3.x ×1
quartile ×1
rank ×1
scipy ×1
statistics ×1