sandy4321 / baseballdbr Goto Github PK
View Code? Open in Web Editor NEWThis project forked from keberwein/baseballdbr
License: Other
This project forked from keberwein/baseballdbr
License: Other
--- output: md_document: variant: markdown_github --- <!-- README.md is generated from README.Rmd. Please edit that file --> ```{r, echo = FALSE} knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "README-", warning = FALSE, message=FALSE ) library(baseballDBR) ``` # BaseballDBR [![Build Status](https://travis-ci.org/keberwein/baseballDBR.png?branch=master)](https://travis-ci.org/keberwein/baseballDBR) [![CRAN_Status_Badge](http://www.r-pkg.org/badges/version/baseballDBR)](http://www.r-pkg.org/badges/version/baseballDBR) [![Project Status: Active - The project has reached a stable, usable state and is being actively developed.](http://www.repostatus.org/badges/latest/active.svg)](http://www.repostatus.org/#active) # Install * The latest development version from GitHub: ```{r eval=FALSE} devtools::install_github("keberwein/baseballDBR") ``` # Gathering Data The `baseballDBR` package requires data that is formatted similar to the [Baseball Databank](https://github.com/chadwickbureau/baseballdatabank) or Sean Lahman's [Baseball Database](http://www.seanlahman.com/baseball-archive/statistics/). The `Lahman` package is great source for these data. The package also contains the `get_bbdb()` function, which allows us to download the most up-to-date tables directly from the Chadwick Bureau's GitHub repository. For example, we can easily load the "Batting" table into our R environment. ```{r} library(baseballDBR) get_bbdb(table = "Batting") head(Batting) ``` # Adding Basic Metrics Simple batting metrics can be easily added to any batting data frame. For example, we can add slugging percentage, on-base percentage and on-base plus slugging. Note that OPS and OBP appears as "NA" for the years before IBB was tracked. ```{r} library(baseballDBR) Batting$SLG <- SLG(Batting) Batting$OBP <- OBP(Batting) head(Batting, 3) ``` # Advanced Metrics The package includes a suite of advanced metrics such as wOBA, RAA, and FIP among others. Many of the advanced metrics require multiple tables. For example, the wOBA metric requires the Batting, Pitching, and Fielding tables in order to establish a player's regular defensive position. ```{r} library(baseballDBR) get_bbdb(table = c("Batting", "Pitching", "Fielding")) Batting$wOBA <- wOBA(Batting, Pitching, Fielding, Fangraphs = T) head(Batting, 3) ``` The code above uses [Fangraphs](http://www.fangraphs.com/guts.aspx?type=cn) wOBA values. The default behavior is to uses [Tom Tango's formula](http://www.insidethebook.com/ee/index.php/site/article/woba_year_by_year_calculations/). Other options include `Sep.Leagues`, which may act as a buffer to any bias created by the designated hitter. ```{r} library(baseballDBR) get_bbdb(table = c("Batting", "Pitching", "Fielding")) Batting$wOBA <- wOBA(Batting, Pitching, Fielding, Fangraphs = F, Sep.Leagues = T) head(Batting, 3) ``` We can also produce a data frame that only shows the wOBA multipliers. Notice the Fangraphs wOBA multipliers slightly differ from the Tango multipliers. ```{r} library(baseballDBR) get_bbdb(table = c("Batting", "Pitching", "Fielding")) fangraphs_woba <- wOBA_values(Batting, Pitching, Fielding, Fangraphs=T) head(fangraphs_woba, 3) tango_woba <- wOBA_values(Batting, Pitching, Fielding, Fangraphs=F) head(tango_woba, 3) ``` # Create Local Database A relational database is not needed to work with these data. However, we may want to store the data to be called more quickly at a later time. We can download all of the tables at once with the `get_bbdb()` function and then write them to an empty schema in our favorite database. The example uses a newly created PostgreSQL instance, but other database tools can be used assuming an appropriate R package exists. ```{r, eval=F} library(baseballDBR) library(RPostgreSQL) # Load all tables into the Global Environment. get_bbdb(AllTables = TRUE) # Make a list of all data frames. dbTables <- names(Filter(isTRUE, eapply(.GlobalEnv, is.data.frame))) # Load data base drivers and load all data frames in a loop. drv <- dbDriver("PostgreSQL") con <- dbConnect(drv, host= "localhost", dbname= "lahman", user= "kriseberwein", password = "pissoff") for (i in 1:length(dbTables)) { if (dbExistsTable(con, dbTables[i])) { dbRemoveTable(dbTables[i]) } dbWriteTable(con, name = dbTables[i], value = get0(dbTables[i])) } # Disconnect from database. dbDisconnect(con) rm(con, drv) ```
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.