Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

I have got his below code from the internet and trying to understand it. the code is as follows:

f_score_prob_matches <- function( missinggames, matchday30, foot_model, max_goals = 10, N, limit ){

 

all_final_tables <- data.frame(stringsAsFactors = FALSE)

clubs <- matchday30$club_name

 

conv <- numeric()

for(i in 1:N){

Goals_team_away <- 0

Goals_team_home <- 0

matchday30 <-

for (m in seq_along(missinggames$club_name_home)){

score_prob <- simulate_score_prob(foot_model = foot_model,

homeTeam = missinggames$club_name_home[m],

awayTeam = missinggames$club_name_away[m],

max_goals = max_goals)

 

score <- base::sample(x = score_prob, size = 1, prob = score_prob)

 

Goals_team_home[m] <- which(score_prob == score, arr.ind = T)[1]

 

Goals_team_away[m] <- which(score_prob == score, arr.ind = T)[2]

}

 

missinggames <- missinggames %>% mutate(Goals_team_home = Goals_team_home,

Goals_team_away = Goals_team_away,

Goal_diff_home = Goals_team_home - Goals_team_away,

Goal_diff_away = Goals_team_away - Goals_team_home)

 

missinggames <- map_df(1:nrow(missinggames), ~if(missinggames$Goals_team_home[.x] > missinggames$Goals_team_away[.x])

mutate(missinggames[.x,], points_team_home = 3, points_team_away = 0) else if

(missinggames$Goals_team_home[.x] == missinggames$Goals_team_away[.x])

mutate(missinggames[.x,], points_team_home = 1, points_team_away = 1) else

mutate(missinggames[.x,], points_team_home = 0, points_team_away = 3)

)

 

final_points <- map(unique(matchday30$club_name), ~ missinggames %>%

filter(club_name_home == .x ) %>%

select(points_team_home) %>%

sum()+ missinggames %>%

filter(club_name_away == .x ) %>%

select(points_team_away) %>%

sum()) %>%

unlist %>%

mutate(matchday30, points_new = .) %>%

mutate(points = points + points_new) %>% select(points)

 

final_goal_diff <- map(unique(matchday30$club_name), ~ missinggames %>%

filter(club_name_home == .x ) %>%

select(Goal_diff_home) %>%

sum()+ missinggames %>%

filter(club_name_away == .x ) %>%

select(Goal_diff_away) %>%

sum()) %>%

unlist %>%

mutate(matchday30, goal_diff_new = .) %>%

mutate(goal_diff = goal_diff + goal_diff_new ) %>%

select(goal_diff)

 

final_table <- matchday30 %>%

select(club_name) %>%

mutate(points = final_points$points,

goal_diff = final_goal_diff$goal_diff) %>%

arrange(desc(points), desc(goal_diff)) %>%

mutate(rank = 1:16)%>%

select(rank, everything())

 

all_final_tables <- bind_rows(all_final_tables,final_table)

average_table <- aggregate(

all_final_tables[1:(length(clubs)*(i-1)),-1],

by = list(all_final_tables$club_name[1:(length(clubs)*(i-1))]),

FUN = "mean"

)

average_table2 <- aggregate(

all_final_tables[,-1],

by = list(all_final_tables$club_name),

FUN = "mean"

)

conv_speed <- sum(abs(average_table$score-average_table2$score))

conv <- c(conv, conv_speed)

if(i %% 10 == 0){

message("convergence speed: ", round(conv_speed, 3), " run ", i, " out of ", N)

}

if(conv_speed < limit){

message("converged!")

break

}

}

if(conv_speed < limit){

conv_plot <- qplot(x=1:length(conv), y=conv, geom = "jitter",

main = paste0("converged to below ", limit,

" after ", length(conv), " runs"))

} else {

conv_plot <- qplot(x=1:length(conv), y=conv, geom = "jitter",

main = paste0("didn't converge below ", limit,

" after ", N, " runs"))

}

return(list(all_final_tables, conv_plot))

}

But I am getting the error telling unmatch bracket in the line 1,29,34,87 only after saving the file. As per my knowledge line number 29-34 might be the hook to the error.

Can anyone guide me?

1 Answer

0 votes
by (36.8k points)

In the below code I am using the case_when method:

library(dplyr)

missinggames %>%

  mutate(points_team_home = case_when(Goals_team_home > Goals_team_away ~3, 

                                      Goals_team_home == Goals_team_away ~ 1, 

                                      TRUE ~ 0), 

         points_team_away = case_when(Goals_team_home > Goals_team_away ~0, 

                                      Goals_team_home == Goals_team_away ~ 1, 

                                      TRUE ~ 3))

 If you are a beginner and want to know more about Data Science the do check out the Data Science course

Browse Categories

...