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

Calculate skew and kurtosis by year in R

我有一个 table 看起来像这样:

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

('Day' 列不参与我的偏度和峰度计算,它只在我的 table 中)

我想要一个 table 来计算按年份分组的偏斜和峰态值:

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"))

我不知道如何按年份分组来执行这些计算。

一个选项是group_by/summarise

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

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

您还可以定义 skewness/kurtosis 函数:

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
}

然后,在 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