2017-11-22 4 views
0

Ich versuche, den Rahmen von diesem Blogpost http://www.luishusier.com/2017/09/28/balance/ mit dem folgenden Code zu reproduzieren, aber es sieht aus wie ich inkonsistente ErgebnisseNormalisieren Daten mit dplyr mutieren() bringen Inkonsistenzen

library(tidyverse) 
library(magrittr) 

ids <- c("1617", "1516", "1415", "1314", "1213", "1112", "1011", "0910", "0809", "0708", "0607", "0506") 

data <- ids %>% 
    map(function(i) {read_csv(paste0("http://www.football-data.co.uk/mmz4281/", i ,"/F1.csv")) %>% 
     select(Date:AST) %>% 
     mutate(season = i)}) 

data <- bind_rows(data) 

data <- data[complete.cases(data[ , 1:3]), ] 

tmp1 <- data %>% 
    select(season, HomeTeam, FTHG:FTR,HS:AST) %>% 
    rename(BP = FTHG, 
     BC = FTAG, 
     TP = HS, 
     TC = AS, 
     TCP = HST, 
     TCC = AST, 
     team = HomeTeam)%>% 
    mutate(Pts = ifelse(FTR == "H", 3, ifelse(FTR == "A", 0, 1)), 
     Terrain = "Domicile") 

tmp2 <- data %>% 
    select(season, AwayTeam, FTHG:FTR, HS:AST) %>% 
    rename(BP = FTAG, 
     BC = FTHG, 
     TP = AS, 
     TC = HS, 
     TCP = AST, 
     TCC = HST, 
     team = AwayTeam)%>% 
    mutate(Pts = ifelse(FTR == "A", 3 ,ifelse(FTR == "H", 0 , 1)), 
     Terrain = "Extérieur") 

tmp3 <- bind_rows(tmp1, tmp2) 

l1_0517 <- tmp3 %>% 
    group_by(season, team)%>% 
    summarise(j = n(), 
      pts = sum(Pts), 
      diff_but = (sum(BP) - sum(BC)), 
      diff_t_ca = (sum(TCP, na.rm = T) - sum(TCC, na.rm = T)), 
      diff_t = (sum(TP, na.rm = T) - sum(TC, na.rm = T)), 
      but_p = sum(BP), 
      but_c = sum(BC), 
      tir_ca_p = sum(TCP, na.rm = T), 
      tir_ca_c = sum(TCC, na.rm = T), 
      tir_p = sum(TP, na.rm = T), 
      tir_c = sum(TC, na.rm = T)) %>% 
    arrange((season), desc(pts), desc(diff_but)) 

Dann erhalten wende ich den Rahmen oben genannten :

l1_0517 <- l1_0517 %>% 
    mutate(

    # First, see how many goals the team scores relative to the average 
    norm_attack = but_p %>% divide_by(mean(but_p)) %>% 
     # Then, transform it into an unconstrained scale 
     log(), 
    # First, see how many goals the team concedes relative to the average 
    norm_defense = but_c %>% divide_by(mean(but_c)) %>% 
     # Invert it, so a higher defense is better 
     raise_to_power(-1) %>% 
     # Then, transform it into an unconstrained scale 
     log(), 

    # Now that we have normalized attack and defense ratings, we can compute 
    # measures of quality and attacking balance 

    quality = norm_attack + norm_defense, 
    balance = norm_attack - norm_defense 
) %>% 
arrange(desc(norm_attack)) 

Wenn ich die Spalte sehen norm_attack, erwarte ich den gleichen Wert für gleichwertige but_p Werte zu finden, was hier nicht der Fall ist:

zum Beispiel wenn but_p Wert hat 83, Reihe 5 und Zeile 7, ich norm_attack bei 0.5612738 und 0.5128357 ist.

Ist das normal? Ich würde erwarten, mean(l1_0517$but_p) zu fixieren und damit das gleiche Ergebnis zu erhalten, wenn ein Wert von l1_0517$but_p Protokoll normalisiert ist?

UPDATE

Ich habe versucht, auf ein einfacheres Beispiel zu arbeiten, aber ich dieses Problem nicht reproduzieren können:

df <- tibble(a = as.integer(runif(200, 15, 100))) 

df <- df %>% 
    mutate(norm_a = a %>% divide_by(mean(a)) %>% 
      log()) 

Antwort

1

ich die Lösung nach einem Blick auf die Art der l1_0517 gefunden

Es ist ein grouped_df daher die unterschiedlichen Ergebnisse.

Der richtige Code ist:

l1_0517 <- tmp3 %>% 
    group_by(season, team)%>% 
    summarise(j = n(), 
      pts = sum(Pts), 
      diff_but = (sum(BP) - sum(BC)), 
      diff_t_ca = (sum(TCP, na.rm = T) - sum(TCC, na.rm = T)), 
      diff_t = (sum(TP, na.rm = T) - sum(TC, na.rm = T)), 
      but_p = sum(BP), 
      but_c = sum(BC), 
      tir_ca_p = sum(TCP, na.rm = T), 
      tir_ca_c = sum(TCC, na.rm = T), 
      tir_p = sum(TP, na.rm = T), 
      tir_c = sum(TC, na.rm = T)) %>% 
    ungroup() %>% 
    arrange((season), desc(pts), desc(diff_but)) 
Verwandte Themen