简体   繁体   English

在 R 中按年份计算偏斜和峰度

[英]Calculate skew and kurtosis by year in R

I have a table that looks like this:我有一个看起来像这样的表:

start_table <- data.frame("Water_Year" =  c("1903", "1903", "1904", "1904"), "X" = c(13, 11, 12, 
15), "Day" = c(1, 2, 1, 2))

(The 'Day' column is not involved in my skew & kurtosis calculation, it is just in my table) (“天”列不参与我的偏斜峰度计算,它只是在我的表中)

I would like a table that calculates the skew and kurtosis values grouped by year:我想要一个计算按年份分组的偏斜和峰度值的表:

end_table <- data.frame("Water_Year" =  c("1903", "1904"), "Skew" = c("skew_number_here", 
"skew_number_here"), "Kurtosis" = c("kurtosis_number_here", "kurtosis_number_here"))

I can't figure out how to group it by year to perform these calculations.我不知道如何按年份对其进行分组以执行这些计算。

Using fBasics with data.table :fBasicsdata.table一起使用:

library(fBasics)
library(data.table)
setDT(start_table)[, .(Skew = skewness(X), Kurtosis=kurtosis(X)), .(Water_Year)][]
#>    Water_Year Skew Kurtosis
#> 1:       1903    0    -2.75
#> 2:       1904    0    -2.75

You can also define the skewness/kurtosis functions:您还可以定义偏度/峰度函数:

kurtosis <- function(x) {  
 m4 <- mean((x - mean(x))^4) 
 kurtosis <- m4/(sd(x)^4) - 3  
 kurtosis
}

skewness <-  function(x) {
 m3 <- mean((x - mean(x))^3)
 skewness <- m3/(sd(x)^3)
 skewness
}

Then, to apply it in base R :然后,将其应用于base R

aggregate(X ~ Water_Year, 
          FUN = function(x) c(kurtosis = kurtosis(x), skewness = skewness(x)),
          data = start_table)

  Water_Year X.kurtosis X.skewness
1       1903      -2.75       0.00
2       1904      -2.75       0.00

An option is group_by/summarise一个选项是group_by/summarise

library(dplyr)
library(moments)
start_table %>% 
   group_by(Water_Year) %>%
   summarise(Skew = skewness(X), Kurtosis = kurtosis(X))

You can use dplyr and e0171 to achieve what you want.您可以使用dplyre0171来实现您想要的。 I generated a mock data set to illustrate this:我生成了一个模拟数据集来说明这一点:

library(dplyr)
set.seed(123)

start_table <- data.frame(year = c(rep(1903, 365), rep(1904, 365)),
                          day = rep(1:365, 2), 
                          X = c(rlnorm(365), rlnorm(365)))


start_table <- start_table %>% group_by(year) %>% 
  summarise(kurt = e1071::kurtosis(X),
            skew = e1071::skewness(X))

Output Output

# A tibble: 2 x 3
   year  kurt  skew
  <dbl> <dbl> <dbl>
1  1903  38.6  4.80
2  1904  13.0  3.10

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM