---
title: "ACCUMEN DATA EXERCISE CODING"
author: "JIASHU MIAO"
date: "2019/1/20"
output: html_document
---



```{r}
library("readr")
require("readxl")
library("dplyr")
library("ggplot2")

```


## Understanding the Data											
a) Are all the values in the data reasonable? Are there missing values?											
b) What are the characteristics of employees at Company A? Do these demographics change over time?											
Use tables and charts to understand the data and demographic characteristics of employees at Company A. 											
```{r}
data_original <- read_excel("/Users/MichaelMiao/Documents/career/accument\ data\ excercise/Acumen_Data_Analysis_Exercise.xlsx", sheet = "Data")
attach(data_original)
#View(data_original)
par(mfrow=c(2,5))
for (i in 1:length(data_original)) {
        boxplot(data_original[,i], main=names(data_original[i]), type="l")

}
```
```{r}
data_original$HealthScore <- 
replace(data_original$`Health Score`,data_original$`Health Score`>6,NA) %>% as.data.frame()
range(data_original$HealthScore,na.rm = T)
data_original <- select_at(data_original, vars(-c(`Health Score`))) %>% as.data.frame()
# data2016new$re[data2016new$re==TRUE]=2
# mutate(datao,dd = replace(Age, (Age>100 | Age < 16),NA))


# data_original %>% select_at(.,vars(c("Age","HealthScore")))
# data_original$`Health Score`[replace(data_original$`Health Score`,data_original$`Health Score`>6,NA)]


# data_original <- data_original %>% mutate(.,rr=replace(data_original$`Health Score`,data_original$`Health Score` > 6,NA))
# data_original

```
```{r}

# so there might be unreasonable variables in Age, Health Score
detach(data_original)
dataNEW1 <-  subset(data_original,`Health Score` >=0 & `Health Score` <=6)
range(data_original$Age)
dataNEW2 <- subset(dataNEW1, dataNEW1$Age >= 18 & dataNEW1$Age <= 63 )
summary(dataNEW2)
dataNEW2 <- na.omit(dataNEW2)
colSums(is.na(dataNEW2))
# filter out the observations that is not in correct score range 0:6, some of them 10!
# filter out the observations that the employ is beyond age 18 or over 62. 
# dataNEW2 becomes the new data


```
```{r}

sum(is.na(data_original$`Sex (Male=1)`))
table(data_original$Race)
```
# we could see there are 1973 missing values for the varible "Racec" at our new dataNEW2. 
#z we could remove them or impuate with other numbers or NULL. 
quartermeanhealth <- aggregate(dataNEW2$`Health Score`,by = list(dataNEW2$Quarter), FUN = mean)
quartermeanhealth <- as.data.frame(quartermeanhealth)
colnames(quartermeanhealth) <- c("Quarter","QuarterMeanHealthScore")
quartermeanhealth
summary(dataNEW2)
ggplot(quartermeanhealth,aes(x=quartermeanhealth$Quarter,y=quartermeanhealth$QuarterMean))+geom_point(size=2)+geom_smooth(method = lm)


saqmean <- aggregate(dataNEW2$Salary, by = list(dataNEW2$Quarter), FUN = mean) %>% as.data.frame()
colnames(saqmean) <- c("Quarter", "QuarterMeanSalaries")
saqmean

# We can see as the time goes by, which is when quartr goes from 1 to 12, the salaries increases and the health score increases based on the means from each quarters as we calculated above.






2. Exploring Relationships
a) Which characteristics are associated with the health score? 
Use tables and charts (suggestion: scatter plots for continuous variables) to determine which characteristics are associated with the health score.

```{r}
datapair <- dataNEW2[sample(nrow(dataNEW2),80),] %>% as.data.frame()
pairs(datapair)
colSums(is.na(dataNEW2))
dim(dataNEW2)
dataNEW2$`Sex (Male=1)` <- as.factor(dataNEW2$`Sex (Male=1)`) 
dataNEW2$Race <- as.factor(dataNEW2$Race)
dataNEW2$`Hospital Visit This Quarter (1=Yes)` <- as.factor(dataNEW2$`Hospital Visit This Quarter (1=Yes)`)
str(dataNEW2)
model1 <- lm(data = dataNEW2,formula = dataNEW2$`Health Score` ~ dataNEW2$Quarter+dataNEW2$Age+dataNEW2$Salary+dataNEW2$`Sex (Male=1)`+dataNEW2$Race+dataNEW2$`Hospital Visit This Quarter (1=Yes)`)
summary(model1)

# Based on the plot Age, SEX, RACE, HOSPITAL VISITS matters with HEALTH SCORE.

model2 <- lm(dataNEW2$`Health Score`~ dataNEW2$Age+dataNEW2$`Sex (Male=1)`+dataNEW2$Race+dataNEW2$`Hospital Visit This Quarter (1=Yes)`)
summary(model2)
par(mfrow=c(2,2))
plot(model2) 
# follow the model assumption. 

plotage <- plot(dataNEW2$Age,dataNEW2$`Health Score`)
plotage
abline(model2)
plot(dataNEW2$Quarter,dataNEW2$`Health Score`) 
attach(dataNEW2)

```

```{r}

```


```{r}
as.data.frame(table(dataNEW2$`Employee Id`))
as.data.frame(table(dataNEW1$Quarter))
dataNEW3 <- subset(dataNEW1, dataNEW1$Quarter == 1) %>% as.data.frame()
range(dataNEW3$`Employee Id`)

as.data.frame(table(data_original$Quarter))
dim(data_original)
plot(table(data_original$Quarter))

```
···




```{r}


attach(data_original)
mydata <- data_original
#Example 1 : Selecting Random N Rows
sample_n(data_original,3)
#Example 2 : Selecting Random Fraction of Rows
sample_frac(mydata,0.001)
attach(mydata)
typeof(mydata$Race)
table(Race)
colSums(is.na(mydata))
summary(mydata)


#Example 3 : Remove Duplicate Rows based on all the variables (Complete Row)

#The distinct function is used to eliminate duplicates.
#x11 = distinct(mydata)

# Example 4 : Remove Duplicate Rows based on a variable
# 
# The .keep_all function is used to retain all other variables in the output data frame.
#x22 = distinct(mydata, race, .keep_all= TRUE)
#x22
# 
# quarter_selected <- select(mydata,mydata$`Sex (Male=1)`)


# Helpers	Description
# starts_with()	Starts with a prefix
# ends_with()	Ends with a prefix
# contains()	Contains a literal string
# matches()	Matches a regular expression
# num_range()	Numerical range like x01, x02, x03.
# one_of()	Variables in character vector.
# everything()	All variables.
```


```{r}
# learn different type of packages. 

#lubridate package that makes you date effient 
#install.packages("lubridate")
#install.packages("rvest")
library("lubridate")
library("rvest")


# mydate1 <- "6aug2005"
# as.data
# mdy(mydate1)
# use rvest to scrape down important info from a web. 
mydata <- as.data.frame(mydata) 

as.data.frame(matrix(unlist(mydata)))



```
```{r}
mydata <- airquality
testdata <- mydata
class(airquality)
mydata <- airquality
mydataozone <- subset(mydata, mydata$Month == 9 | mydata$Month == 5)
mydata2 <- subset(mydata[,1:3])
mydata4 = select(mydata, contains("day")) # this is cool
mydata5 = select(mydata, Day) # this neng yong le 
# 
# Example 10 : Reorder Variables
# 
# The code below keeps variable 'State' in the front and the remaining variables follow that.
# mydata5 = select(mydata, State, everything())

mydata5 <- select(mydata,Day,everything())
head(mydata5,0)

# rename() syntax : rename(data , new_name = old_name)
# data : Data Frame
# new_name : New variable name you want to keep
# old_name : Existing Variable Name
mydata6 <- rename(mydata5, daysssss=Day)
head(mydata6,1)

# EG filter

mydata7 <- filter(mydata, Day %in% (5:9))
mydata7 <- filter(mydata, Temp == 90)

# mydata7 = filter(mydata6, Index %in% c("A", "C"))

# Example 17 : CONTAINS Condition

# The grepl function is used to search for pattern matching. In the following code, we are looking for records wherein column state contains 'Ar' in their name.
# mydata10 = filter(mydata6, grepl("Ar", State))

#summarise_at(mydata, vars(Y2005, Y2006), funs(n(), mean, median))
#summarise(mydata, Y2015_mean = mean(Y2015), Y2015_med=median(Y2015))
summarise(mydata,n())

#summarise_at(mydata, vars(Y2011, Y2012),
#funs(n(), missing = sum(is.na(.)), mean(., na.rm = TRUE), median(.,na.rm = TRUE)))
sample(1:100,4,replace = F)
runif(4)


## summarize all numerice

summarise_all(mydata,funs(mean,median))
summarize_if(mydata,is.numeric,funs(n(),min(.,na.rm = T))) #na.rm = T


# alternative way

numdata <- mydata[sapply(mydata, is.numeric)]
summarise_all(numdata,funs(n(),mean(.,na.rm = F))) # na.rm = T


# We are checking the number of levels/categories and count of missing observations in a categorical (factor) variable.
# summarise_all(mydata["Index"], funs(nlevels(.), nmiss=sum(is.na(.))))
mydata[c("Day","Ozone")][,1:2]


#arrange function 
# Syntax
# arrange(data_frame, variable(s)_to_sort)
# or
# data_frame %>% arrange(variable(s)_to_sort)

arrange(mydata,Month,Day)


# pipe
# dt = sample_n(select(mydata, Index, State),10)
# or 
# dt = mydata %>% select(Index, State) %>% sample_n(10)


# t = summarise_at(group_by(mydata, Index), vars(Y2011, Y2012), funs(n(), mean(., na.rm = TRUE)))
# The above code can also be written like
# t = mydata %>% group_by(Index) %>%
#   summarise_at(vars(Y2011:Y2015), funs(n(), mean(., na.rm = TRUE)))

do()

# max 3


# t = mydata %>% select(Index, Y2015) %>%
#   filter(Index %in% c("A", "C","I")) %>%
#   group_by(Index) %>%
#   do(arrange(.,desc(Y2015))) %>%  slice(3)

mydata8 <- mydata %>% select(Day,Month,Temp) %>%
  filter(Day %in% (1:20) | Month >= 6) %>% 
  group_by(Month) %>% do(arrange(.,desc(Day))) %>% slice(.,2) # 第二名  
mydata8 
# do 加不加

```
```{r}
# put
# Using Window Functions
# 
# Like SQL, dplyr uses window functions that are used to subset data within a group. It returns a vector of values. We could use min_rank() function that calculates rank in the preceding example,
# t = mydata %>% select(Index, Y2015) %>%
#   filter(Index %in% c("A", "C","I")) %>%
#   group_by(Index) %>%
#   filter(min_rank(desc(Y2015)) == 3)


#(min_rank(desc(Y2015)) == 3) 这时候 filter function要用

# create function
mydata9 <- mutate(mydata,ratio= Temp/Day)


# Example 29 : multiply all the variables by 1000

mydata10 <- mutate_all(mydata,funs("new"=.*1000))
mydata10



# Solution : See Example 45 - Apply multiplication on only numeric variables
# 
# Example 30 : Calculate Rank for Variables
# 
# Suppose you need to calculate rank for variables Y2008 to Y2010.
# mydata12 = mutate_at(mydata, vars(Y2008:Y2010), funs(Rank=min_rank(.)))
# 
# Output
# By default, min_rank() assigns 1 to the smallest value and high number to the largest value. In case, you need to assign rank 1 to the largest value of a variable, use min_rank(desc(.))
# mydata13 = mutate_at(mydata, vars(Y2008:Y2010), funs(Rank=min_rank(desc(.))))


# index cumulative sum
#cumsum(mydata$Temp) 


# join 
#inner_join(x,y,by="ID")
# left, right, full, semi, anti

# Combine data vertically 
# rows in both x and y 
mydata11 <- intersect(mydata10,mydata2)

# rows in either or both x, y
mydata12 <- union(mydata10,mydata2) %>%  as.data.frame()

# rows in x but NOT in y
mydata13 <- setdiff(mydata11,mydata)


## Example 38

# if_else(condition,true,flase,missing = NULL)
# if_else(df<0, "negative","positive",missing = "missing vcalue")
```



```{r}

df <- data.frame(x = c(1,5,6,NA))
df %>%  mutate(.,newvar = if_else(x<5,x+1,x+2,0))


# Nested IF ELSE
# 
# Multiple IF ELSE statement can be written using if_else() function. See the example below -
# mydf =data.frame(x = c(1:5,NA))
# mydf %>% mutate(newvar= if_else(is.na(x),"I am missing",
# if_else(x==1,"I am one",
# if_else(x==2,"I am two",
# if_else(x==3,"I am three","Others")))))



# SQL-Style CASE WHEN Statement
# 
# We can use case_when() function to write nested if-else queries. In case_when(), you can use variables directly within case_when() wrapper. TRUE refers to ELSE statement. 
# mydf %>% mutate(flag = case_when(is.na(x) ~ "I am missing",
#                                   x == 1 ~ "I am one",
#                                   x == 2 ~ "I am two",
#                                   x == 3 ~ "I am three",
#                                  TRUE ~ "Others"))

# Example 39 :  Apply ROW WISE Operation 
# 
# Suppose you want to find maximum value in each row of variables 2012, 2013, 2014, 2015. The rowwise() function allows you to apply functions to rows.

# df = mydata %>%
  # rowwise() %>% mutate(Max= max(Y2012,Y2013,Y2014,Y2015)) %>%
  # select(Y2012:Y2015,Max)

```
```{r}
# Example 40 : Combine Data Frames
# 
# Suppose you are asked to combine two data frames. Let's first create two sample datasets.

df1=data.frame(ID = 1:6,  x=letters[1:6])
df2=data.frame(ID = 7:12, x=letters[7:12])

# Input Datasets
# The bind_rows() function combine two datasets with rows. So combined dataset would contain 12 rows (6+6) and 2 columns.
xy1 = bind_rows(df1,df2)
# It is equivalent to base R function rbind.

xy2 = rbind(df1,df2)
# The bind_cols() function combine two datasets with columns. So combined dataset would contain 4 columns and 6 rows.
xy3 = bind_cols(df1,df2)
#or
xy4 = cbind(df1,df2)
# The output is shown below-
xy1
xy2
xy3
xy4


# Example 41 : Calculate Percentile Values
# The quantile() function is used to determine Nth percentile value. In this example, we are computing percentile values by variable Index.
# 
# mydata %>% group_by(Index) %>%
#   summarise(Pecentile_25=quantile(Y2015, probs=0.25),
#             Pecentile_50=quantile(Y2015, probs=0.5),
#             Pecentile_75=quantile(Y2015, probs=0.75),
#             Pecentile_99=quantile(Y2015, probs=0.99))
# The ntile() function is used to divide the data into N bins.
# 
# x= data.frame(N= 1:10)
# x = mutate(x, pos = ntile(x$N,5))
```

```{r}

by_cyl <- group_by(mtcars, cyl)
models <- by_cyl %>% do(mod = lm(mpg ~ disp, data = .))
summarise(models, rsq = summary(mod)$r.squared)
models %>% do(data.frame(
  var = names(coef(.$mod)),
  coef(summary(.$mod)))
)
#group_by(data,col)!!!!


# The select_if() function returns only those columns where logical condition is TRUE. The is.numeric refers to retain only numeric variables.
# 
# mydata2 = select_if(mydata, is.numeric)
# Similarly, you can use the following code for selecting factor columns - 
summarise_if(mydata, is.factor, funs(nlevels(.)))


# Example 46 : Convert value to NA
# 
# In this example, we are converting "" to NA using na_if() function.
k <- c("a", "b", "", "d")
na_if(k, "")
# Result : "a" "b" NA  "d"



```
```{r}

nrow(mydata)
ncol(mydata)
names(mydata)
names(mydata)
head(mydata,3)

mydata = fread("https://github.com/arunsrinivasan/satrdays-workshop/raw/master/flights_2014.csv")
```
```{r}
# Suppose you need to select only 'origin' column. You can use the code below -
dat1 = mydata[ , origin] # returns a vector
#dat1


# To get result in data.table format, run the code below :
dat1 = mydata[ , .(origin)] # returns a data.table
# It can also be written like data.frame way
dat1 = mydata[, c("origin"), with=FALSE]



#Keeping a column based on column position
dat2 =mydata[, 2, with=FALSE]

# The following code tells R to select 'origin', 'year', 'month', 'hour' columns.
dat3 = mydata[, .(origin, year, month, hour)]

# Keeping multiple columns based on column position
# 
# You can keep second through fourth columns using the code below -
dat4 = mydata[, c(2:4), with=FALSE]


# Dropping a Column

# Dropping Multiple Columns
dat6 = mydata[, !c("origin", "year", "month"), with=FALSE]

# Keeping variables that contain 'dep'
# 
# You can use %like% operator to find pattern. It is same as base R's grepl() function, SQL's LIKE operator and SAS's CONTAINS function.
# dat7 = mydata[,names(mydata) %like% "dep", with=FALSE]


# grep(pattern, x, ignore.case = FALSE, perl = FALSE, value = FALSE,
#      fixed = FALSE, useBytes = FALSE, invert = FALSE)
# 
# grepl(pattern, x, ignore.case = FALSE, perl = FALSE,
#       fixed = FALSE, useBytes = FALSE)

# Faster Data Manipulation with Indexing
# 
# data.table uses binary search algorithm that makes data manipulation faster.

dat1.1 <- setkey(mydata,origin)


# We can also set keys to multiple columns like we did below to columns 'origin' and 'dest'. See the example below.
# setkey(mydata, origin, dest)
# Filtering while setting keys on Multiple Columns
# First key column 'origin' matches “JFK” and second key column 'dest' matches “MIA”
mydata[.("JFK", "MIA")]
# It is equivalent to the following code :
mydata[origin == "JFK" & dest == "MIA"]



```
```{r}
ggplot(dataNEW2, aes(x=round(dataNEW2$Age,0), y=dataNEW2$`Health Score`)) + geom_point(alpha=0.2) + theme_classic()

```


```{r}


#review one more time
sample(1:5,5,replace = F)
mydata <- mtcars
sample_n(mydata,2)
sample_frac(mydata,0.2) # rows
distinct(mydata) # remove duplicate rows
distinct(mydata,carb, .keep_all= TRUE) # Remove duplicate rows of the dataframe using carb variable
distinct(mydata, cyl,vs, .keep_all= TRUE) #Remove duplicate based on multiple variables.

select(mydata,mpg,cyl,wt) # Select columns of the dataframe

# Select 3rd and 4th columns of the dataframe
select(mydata,3:4)
# 各种 


library(dplyr)
mydata <- mtcars
 
# Select everything
select(mydata,everything())


# Drop the columns of the dataframe
select (mydata,-c(mpg,cyl,wt))

# Drop 3rd,4th and 5th columns of the dataframe
select(mydata,-c(3,4,5))
select(mydata,-ends_with("cyl"))
select(mydata,-contains("s"))
Mydata1 = select(mydata, gear,hp,qsec,vs, everything())
#Mydata1

# Rename the column name of the dataframe
Mydata1 = rename(mydata, displacement=disp, cylinder=cyl)

# subset the rows of dataframe with condition
Mydata1 = filter(mydata,cyl==6)
Mydata1
# subset the rows of dataframe with multiple conditions
Mydata1 = filter(mydata, gear %in% c(4,5))
# subset the rows of dataframe with with Contains condition
Mydata1 = filter(mydata, grepl(0,hp))


# summarise the columns of dataframe
summarise(mydata, mpg_mean=mean(mpg,rm.na=T),mpg_median=median(mpg))
# summarise the list of columns of dataframe
summarise_at(mydata, vars(mpg, hp), funs(n(), mean, median))
# summarise all the column of dataframe
summarise_all(mydata,funs(n(),mean,median))
# summarise all the list of numeric variable of dataframe
summarise_if(mydata, is.numeric, funs(n(),mean,median))
#library(dplyr)
mydata2 <- iris
summarise_all(mydata2["Species"], funs(nlevels(.), nmiss=sum(is.na(.))))
#### vars!!!

# sort the dataframe in R
arrange(mydata,mpg)
# Groupby function for dataframe in R
summarise_at(group_by(mydata2,Species),vars(Sepal.Length),funs(mean(.,na.rm=TRUE)))



mydata2 <-iris 
# Group by function for dataframe in R using pipe operator 
mydata2 %>% group_by(Species) %>% summarise_at(vars(Sepal.Length),funs(sum(.,na.rm=TRUE)))


# Groupby in R without dplyr using aggregate function:
# 
# In this example we will be using aggregate function in R to do group by operation as shown below

mydata2 <-iris 
 
# Group by in R using aggregate function
 
aggregate(mydata2$Sepal.Length, by=list(Species=mydata2$Species), FUN=sum)


# library(dplyr)
mydata2 <-iris # windows function in R using Dplyr 
mydata2 %>% 
select(Species,Sepal.Length) %>%
    group_by(Species) %>% # 只是让你看一个gourp都在一起
    filter(min_rank(desc(Sepal.Length))<=5) # top five of each group

# By the way, I’m using ‘min_rank()’ function here but there is another rank function called ‘dense_rank()’ from dplyr. Both functions return the ranking number based on a given measure column (e.g. Sepal.Length), and only the difference is when there are ties like below.


# dense_rank() 的时候 俩并列算作一个位置 接下来的排名顺序不变to 

#a.agg <- aggregate(X.2 ~ X.1, a, function(x) sort(x, decreasing=T)[2])
# (df.second <- merge(a.agg, a))
## S3 method for class 'formula'
# aggregate(formula, data, FUN, ...,
#           subset, na.action = na.omit)

library(dplyr)
 iris %>%
      group_by(Species) %>%
      distinct(Sepal.Length)%>% 
      arrange(.,desc(Sepal.Length)) %>% 
      slice(1:2) %>% 
      select(Sepal.Length)


 
 
 
 # mutate(data_frame, expression(s) )
# or
# data_frame %>% mutate(expression(s)
 
 # Mutate_all function for creating new variable to the dataframe in R
 
mydata3 = mutate_all(mydata2[,-5], funs("percent"= ./100))
head(mydata3) #yong all!!!
# mutate_at() function for creating new variable to the dataframe in R
mydata4 = mutate_at(mydata2, vars(Sepal.Length,Sepal.Width), funs(Rank=min_rank(desc(.))))
head(mydata4)





df1 = data.frame(CustomerId = c(1:6), Product = c(rep("Oven", 3), rep("Television", 3)))
df2 = data.frame(CustomerId = c(4:7), Product = c(rep("Television", 2), rep("Air conditioner", 2)))
#  union two dataframes  without duplicates
union(df1,df2)

#  union two dataframes  with duplicates
union_all(df1,df2)

intersect(df1,df2)

#  difference of two dataframes  
setdiff(df1,df2)



# We will be creating additional variable species_new using mutate function and case when statement.TRUE equivalent to ELSE statement
mydata2 %>% mutate(species_new = case_when(is.na(Species) ~ "missing",
                                           Species=="setosa" ~ "setosa_new",
                                           Species=="versicolor" ~ "versicolor_new",
                                           Species=="virginica" ~ "virginica_new",
                                           TRUE ~ "others"))

# 
# We will be creating additional variable row_max using mutate function and rowwise() function to store the row wise maximum variable.

df1 = mydata2 %>%
  rowwise() %>% mutate(row_max= max(Sepal.Length:Petal.Width))
 
head(df1)
mydata<-mtcars
df1 = mutate(mydata, percentile_rank = ntile(mydata$mpg,100))
df1
 
```



The dot (.) denotes each variables specified in the second argument of the function.
summarise_at(mydata, vars(Y2011, Y2012),
funs(n(), missing = sum(is.na(.)), mean(., na.rm = TRUE), median(.,na.rm = TRUE)))






 Evaluating the Claim
a) Using the information from Questions 1 and 2, describe how you would evaluate InsurAHealth's claim that employees are getting sicker.
First list how you would evaluate the claim. Then, time-permitting, implement the steps you suggested. 

- I would agree with the claim, by fitting a regression model for the health score with variales age, sex, salary, race, quarters and hopitable visits we find the model2 works just fine and which tells the as ages goes up, the value of health score increases -- it means people become sicker, based on the scale :  0 denotes a very healthy person, and 6 denotes a very sick person. 








<!-- let as clean out the NA in dataNEW2 for the race group, we do that by replace NA with NULL  -->
<!-- dataUSE <- sapply(dataNEW2, as.character) -->
<!-- dataUSE <- as.data.frame(dataUSE) -->
<!-- colSums(is.na(dataUSE)) -->
<!-- dataDraft <- dataUSE -->
<!-- class(dataUSE) -->
<!-- str(dataUSE) -->
<!-- boxplot(dataUSE$`Sex (Male=1)`,as.numeric(as.character(dataUSE$`Health Score`))) -->
<!-- #We would like to change all columns types to numeric -->
<!-- dataUSE[,c(1,2,4,6,8,9)] <- sapply(dataUSE, function(x) as.numeric(as.character(x))) -->
<!-- str(dataUSE) -->
<!-- # get rid of the NAs because NULL becomes NA again.  -->

<!-- colSums(is.na(dataUSE)) -->
<!-- dim(dataUSE) -->

<!-- model1 <- lm(data = dataUSE, formula = dataUSE$`Health Score` ~ dataUSE$Quarter+dataUSE$Age+dataUSE$Salary+dataUSE$`Hospital Visit This Quarter (1=Yes)`+dataUSE$`Sex (Male=1)`+dataUSE$`Hospital Visit This Quarter (1=Yes)`+dataUSE$Race) -->

<!-- summary(model1) -->
<!-- sum(is.na(dataUSE$`Sex (Male=1)`)) -->

<!-- # Age, Salary -->

<!-- dataUSE <- na.omit(dataUSE) -->
<!-- colSums(is.na(dataUSE)) -->
<!-- model1 <- lm(data = dataUSE, formula = dataUSE$`Health Score` ~ dataUSE$Quarter+dataUSE$Age+dataUSE$Salary+dataUSE$Race+dataUSE$`Sex(Male=1)`) -->






<!-- #                dataUSE$Quarter+dataUSE$Age+dataUSE$Salary+factor(dataUSE$`Sex(Male=1)`)+factor(dataUSE$Race)+factor(dataUSE$`Hospital Visit This Quarter (1=Yes)`)) -->
<!-- summary(model1) -->
<!-- anova(model1) -->
<!-- ``` -->

<!-- #pairs(dataUSE$`Health Score` ~ dataUSE$`Employee Id`+`Sex (Male=1)`+Race+Age+`Hospital Visit This Quarter (1=Yes)`+Salary+Quarter) -->
<!-- #pairs function not working well because of too many oberservations. -->

<!-- #model1 <- lm(formula = dataUSE$`Health Score` ~ Quarter + Salary + dataUSE$`Sex (Male=1)`,data = dataUSE) -->
<!-- # The dataset is too large to run the whole model in my laptop so I take a random sample here.  -->

<!-- #datamodel1 <- dataUSE[sample(nrow(dataUSE),15000),] -->
<!-- #as.data.frame(datamodel1) -->
<!-- #str(datamodel1) -->


<!-- model1 <- lm(data = datamodel1, formula = `Health Score`~ Quarter + Salary + Age + factor(`Sex (Male=1)`)+factor(`Hospital Visit This Quarter (1=Yes)`) ) -->
<!-- summary(model1) -->
<!-- par(mfrow=c(1,2)) -->
<!-- boxplot(datamodel1$`Sex (Male=1)`, datamodel1$`Health Score`) -->
<!-- boxplot(datamodel1$`Hospital Visit This Quarter (1=Yes)`,datamodel1$`Health Score`) -->
