-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_setup.R
More file actions
101 lines (93 loc) · 2.27 KB
/
data_setup.R
File metadata and controls
101 lines (93 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
### Setup Files for Analysis
### By Matt Barger, 3 March 2025
### Run this first. Make sure the .Renviron files are correct
library(odbc)
library(tidyverse)
library(DBI)
library(RPostgres)
options(scipen = 9999)
#Connect to the database
conn <- DBI::dbConnect(RPostgres::Postgres(),
dbname = Sys.getenv("ASA_dbname"),
host = Sys.getenv("ASA_HOST"),
port = 25060,
user = Sys.getenv("ASA_USERNAME") ,
password = Sys.getenv("ASA_PASSWORD"))
#Events - Events for all matches from the 2025 season
events_mls25 <- dbGetQuery(
conn = conn,
"SELECT *
FROM mls.events e
WHERE e.game_id = ANY (
SELECT game_id
FROM mls.games
WHERE season_name = '2025'
AND home_score IS NOT NULL
)"
)
#xgoals_mls25 - Information for all shots for all matches in the 2025 season
xgoals_mls25 <- dbGetQuery(
conn = conn,
"SELECT *
FROM mls.xgoals xg
WHERE xg.game_id = ANY (
SELECT game_id
FROM mls.games
WHERE season_name = '2025'
AND home_score IS NOT NULL
)"
)
#xgoals_mls25 - Information for all shots for all matches in the 2025 season
gplus_mls25 <- dbGetQuery(
conn = conn,
"SELECT *
FROM mls.goals_added ga
WHERE ga.game_id = ANY (
SELECT game_id
FROM mls.games
WHERE season_name = '2025'
AND home_score IS NOT NULL
)"
)
#Player Index - all Player Information
player_index <- dbGetQuery(
conn = conn,
"SELECT *
FROM all_opta.players
WHERE player_id = ANY(
SELECT DISTINCT PLAYER_ID
FROM mls.events e
WHERE e.game_id = ANY (
SELECT game_id
FROM mls.games
WHERE season_name = '2025'
AND home_score IS NOT NULL
)
)"
)
team_index <- dbGetQuery(
conn = conn,
"SELECT *
FROM all_opta.teams
WHERE team_id = ANY(
SELECT DISTINCT TEAM_ID
FROM mls.events e
WHERE e.game_id = ANY (
SELECT game_id
FROM mls.games
WHERE season_name = '2025'
AND home_score IS NOT NULL
)
)"
)
game_index <- dbGetQuery(
conn = conn,
"SELECT *
FROM mls.games g
--WHERE season_name = '2024'
WHERE home_score IS NOT NULL"
)
type_index <- dbGetQuery(
conn = conn,
"select * from all_opta.ref_event_types ret"
)