---
title: "Stat 185 Intro"
# author: "Mark"
# date: "8/22/2017"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## Statistics vs data science

Check out Wikipedia's [article on Data Science](https://en.wikipedia.org/wiki/Data_science). Quoting from that article, I guess that

 * Data science is "The Sexiest Job of the 21st Century" (Wikipedia [cites the Harvard Business Review](https://hbr.org/2012/10/data-scientist-the-sexiest-job-of-the-21st-century/))
 * There could be a global shortage of 1.5 million data scientists (Wikipedia cites [McKinsey & Company](http://www.mckinsey.com/insights/business_technology/big_data_the_next_frontier_for_innovation))
 
So... just what is this hot, new field? Well, according to the same Wikipedia article, 
 
 * Data science is a "concept to unify statistics, data analysis and their related methods" (Wikipedia cites [a prominent text](http://link.springer.com/chapter/10.1007/978-4-431-65950-1_3))
 * Data science is "sexed-up term for statistics" (Wikipedia cites [Nate Silver](http://www.statisticsviews.com/details/feature/5133141/Nate-Silver-What-I-need-from-statisticians.html))

### Why so hot?

Statistics (or data science, if you want) is hot for *good reason*. Data is becoming easier an easier to come by and it's impact more an more pervasive. Think

 * Tech (tracking your browsing history)
 * Finance (analyzing the markets)
 * Sports ([Moneyball](https://en.wikipedia.org/wiki/Moneyball))
 * Medicine (The genome)
 * Politics

## A look at some actual data

Anyone who follows baseball can tell you offensive output varies by defensive position; pitchers are so bad that their position could be considered an outlier. Let's look at some actual data to try to back this up.

### Reading and looking at raw data

On my webspace, I have a data file containing batting statistics for all 1199 players in 2010. Let's take a look:


```{r}
library(knitr)
df = read.csv('https://www.marksmath.org/data/mlbBat10.tsv', sep="\t")
kable(head(df))
```

In the code above, the `read.csv` command reads the data from the table. That data is then stored in a variable called `df` (for DataFrame). The resulting DataFrame has 1199 rows and 19 columns. The `head` command grabs just the first few rows and the `kable` command makes it look a little pretty.

The data has actually already been throught quite a bit of formatting. Still, it's so huge that's quite a challenge to get a grip on it. Statistics provides tools (both qualitative and quantitative) to analyze large datasets like this. 

### A question

First, let's state a specific question we wish to address: *How is on base percentage related to position*?

### Qualitative analysis

One way to *visually* investigate this question is with a side-by-side box and whisker plot.

```{r, echo=F}
library(ggplot2)
df = subset(df, G>75 & position != 'P')
ggplot(df, aes(x = position, y = OBP)) +
   geom_boxplot(fill = "grey80", colour = "blue") +
   scale_x_discrete() + xlab("Position") +
   ylab("On base percentage")
```

In this image, the positions are listed on the horizontal axis and the on base percentage is on the vertical axis. The data has actually been trimmed down quite a bit to include only those non-pitchers who played at least 75 games; there were 327 such players that year. If you understand how to read a box and whikser plot, you can see quite clearly the differences in on base percentage between the different positions

### Quantitative analysis

While we can certainly *see* the differences between the positions in the box and whisker plots, it's nice to have definitive numbers to point to that support our analysis. For the analysis of the variation between several variables, there is a well established tool called ANOVA, for Analysis of Variations. Here's how to run ANOVA for this example:

```{r}
df$position = factor(df$position,
  labels = c('1B', '2B', '3B', 'C', 'DH', 'OF', 'SS')
)
anova(lm(df$OBP ~ df$position))
```

The "Pr(>F)" entry is an example of a $p$-value. As we will learn, these kinds of computations allow us to make inferences. Typically, a small $p$-value indicates a deviation from the status-quo. In this case, it indicates that the batting performances between the positions are not all the same.

## Types of data

There are two main types of data and both types can be further classified into two sub-types.

  * Numerical data, which can be
    * Discrete or
    * Continuous
  * Categorical data, which can be
    * Nominal or 
    * Ordinal

If you take a look at our baseball data above, you can see numerical data of both types as well as Nominal, Categorical data. An example of Ordinal, Categorical data might be the player's jersey number. It looks numeric but there's really no reasonable computation that can be done with it.

---