---
title: "Untitled"
author: "JIASHU MIAO"
date: "4/9/2019"
output: html_document
---
```{r}
pkg <- c("readr","readxl","dplyr","stringr","ggplot2","tidyr")
```

```{r}
pkg <- c("readr","readxl","dplyr","stringr","ggplot2","tidyr","car","caret"

,"ranger"
,"rsample"
,"randomForest")
pkgload <- lapply(pkg, require, character.only = TRUE)

```



```{r}
seq(1,nrow(rpe),10)

gps <- gps %>% group_by(PlayerID) %>% 
  mutate(tackle = (AccelImpulse > 3.5 & Speed < 1 &(AccelX < 0 | AccelY < 0 | AccelZ < 0))) 

gps$group <- rep(1:ceiling(nrow(gps)/10),each=10)
gps$group %>% tail(.,20)

 tapply(gps$PlayerID,gps$GameID,mean)
# seq_len(nrow(gps),length.out = 10)
 # ?seq_len
tackle_num <- gps %>% group_by(GameID,Half,PlayerID,group) %>% 
   summarise(tackle = any(tackle), speedAverage = mean(Speed)) 


tackle_num


tackle_num2 <-  gps %>% group_by(GameID,Half,PlayerID,group) %>% 
   summarise(tackle = any(tackle), speedAverage = mean(Speed)) %>%  summarise(tackle=sum(tackle),speedAverage=mean(speedAverage))
tackle_num2
tackle_num
# summarise第二次就 无视第二个groypby了

```

```{r}
# install.packages("MASS")
library("MASS")

re <- 1 / 9 * (12 + 6 + 28 + 12 + 24 + 34)
fractions(re)


# suppressMessages(library(foo))


# command shit k !!!!
# command ooption (shift )L
# coomnd shift option G
# control f11 f12 ！！！！！
# cmd shift A reform
# cmd f9 f10

#  option up/ down move line

# cmmd left/ right!!!


# option + delete 

# view ctrl + 1、2、3、4

# commna shit left / right 可以选择
               #  	Cmd+Option+O
# Unfold All	Shift+Alt+O	
# Cmd+Shift+Option+O
# Cmd+Shift+Option+O
# Go to line	Shift+Alt+G	Cmd+Shift+Option+G
# Çd ion


# Median imputation is fast, but...
# Can produce incorrect results if data missing not at random


```

```{r}
 data(mtcars)
 mtcars[mtcars$disp < 140, "hp"] <- NA
 Y <- mtcars$mpg
 X <- mtcars[, 2:4]
# Use median imputation
 
 set.seed(42)
 model <- train(x = X, y = Y, method = "glm",
                 preProcess = "medianImpute")
 print(min(model$results$RMSE))

 
 

  set.seed(42)
 model <- train(x = X, y = Y,
                 method = "glm",
                 preProcess = "knnImpute"
  )
 
 print(min(model$results$RMSE))
# Compare to 3.61 for median imputation
```
```{r}
 # Generate some data with missing values
 data(mtcars)
 set.seed(42)
 mtcars[sample(1:nrow(mtcars), 10), "hp"] <- NA
 Y <- mtcars$mpg
 X <- mtcars[,2:4]
# Use linear model "recipe"
 set.seed(42)
 model <- train(
    x = X, y = Y, method = "glm",
    preProcess = c("medianImpute", "center", "scale")
  )
 print(min(model$results$RMSE))
 3.612713
 
 
 # spatial 
 set.seed(42)
 model <- train(
    x = X, y = Y, method = "glm",
    preProcess = c("medianImpute", "center", "scale", "spatialSign"))
 min(model$results$RMSE)

```

```{r}
# PCA before modeling
 set.seed(42)
 model <- train(
    x = X, y = Y, method = "glm",
    preProcess = c("medianImpute", "center", "scale", "pca")
  )
 min(model$results$RMSE)
```

●
● ●
●
Start with median imputation For linear models...
Center and scale
Try KNN imputation if data missing not at random
Try PCA and spatial sign
**Tree-based models don't need much preprocessing**

```{r}
     # Reproduce dataset from last video
 data(mtcars)
 set.seed(42)
 mtcars[sample(1:nrow(mtcars), 10), "hp"] <- NA
 Y <- mtcars$mpg
 X <- mtcars[, 2:4]
# Add constant-valued column to mtcars

  X$bad <- 1
```

```{r}
model <- train(
    x = X, y = Y, method = "glm",
    preProcess = c("medianImpute", "center", "scale", "pca")
  )
# model
```

Make sure they were fit on the same data! Selection criteria
Highest average AUC
Lowest standard deviation in AUC
● ●
● ●
●
The resamples() function is your friend

In Machine Learning, performance measurement is an essential task. So when it comes to a classification problem, we can count on an AUC - ROC Curve. When we need to check or visualize the performance of the multi - class classification problem, we use AUC (Area Under The Curve) ROC (Receiver Operating Characteristics) curve. It is one of the most important evaluation metrics for checking any classification model’s performance. It is also written as AUROC (Area Under the Receiver Operating Characteristics)

```{r}
xyplot(resamples, metric = "ROC")
```

```{r}
summary(model)
```
```{r}
set.seed(42)
 model <- train(
    x = X, y = Y, method = "glm",
    preProcess = c("zv", "medianImpute", "center", "scale", "pca")
  )
 min(model$results$RMSE)
```
```{r}
# install.packages("readtext")
library("readtext")
files="https://kenbenoit.net/pdfs/text_analysis_in_R.pdf"
rt <- readtext(files,text_field = "texts")


rt






```
```{r}
##text analysi 
install.packages("quanteda")
```

```{r}
library("quanteda")


text <- "An example of preprocessing techiniqeus "
toks <- tokens(text)

toks[[1]] %>% length()




```

```{r}
toupper(toks)
```

```{r}
# For example using gsub or sub
# 
#     gsub('.*:(.*)','\\1',string)
#     1] "E001" "E002" "E003"
```
```{r}
sw <- stopwords("english") %>% print
tokens_remove(toks,sw)

```

```{r}
# install.packages("spacyr")
```

```{r}
text

 tokens(text,ngrams = 3,skip = 0:1)
 tokens(text,ngrams = 3,skip = 0:2)
library(quanteda)
library(quanteda)
tokens(text)
```




