---
title: "Petfinder.my"
output:
  html_document:
    df_print: paged
---

Add a new chunk by clicking the *Insert Chunk* button on the toolbar or by pressing *Ctrl+Alt+I*.

When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the *Preview* button or press *Ctrl+Shift+K* to preview the HTML file).

The preview shows you a rendered HTML copy of the contents of the editor. Consequently, unlike *Knit*, *Preview* does not run any R code chunks. Instead, the output of the chunk when it was last run in the editor is displayed.

```{r}
library(ggplot2)
library(dplyr)
```


```{r}
train <- read.csv("../input/train/train.csv", stringsAsFactors = FALSE, header=TRUE)
head(train)
summary(train)
```

#Relabeling
```{r}
train$Type <- factor(train$Type, levels=c("1","2"),labels=c("Dog", "Cat"))
train$AdoptionSpeed <- factor(train$AdoptionSpeed, 
                     levels=c("0","1","2","3","4"),
                     labels=c("Same Day", "1-7 Days", "8-30 Days", "31-90 Days", "100+ Days"))
train$Gender <- factor(train$Gender, 
                     levels=c("1","2","3"),
                     labels=c("Male", "Female", "Mixed"))
train$Color1 <- factor(train$Color1, 
                     levels=c("1","2","3", "4", "5", "6", "7"),
                     labels=c("Black", "Brown", "Golden", "Yellow", "Cream", "Gray", "White"))
```

#Train Dataset Overview

24 variables and 14,993 records.
```{r}
dim(train)
```

# AdoptionSpeed
Most groups are equally balanced, except Same Day only has 2.7%.
```{r}
summary(train$AdoptionSpeed)
p <- ggplot(data=train, aes(x=AdoptionSpeed)) + 
  geom_bar(stat="count")
p
```

# Type
54.24% dogs
45.76% cats
```{r}
summary(train$Type)
ggplot(data=train, aes(x=Type)) + geom_bar(stat="count")
```

#Which are adopted faster, cats or dogs?
They are addopted at about the same rate


Jitter is used, so all the points don't overlap
```{r}
p <- ggplot(data=train, aes(x=Type, y=AdoptionSpeed, col=AdoptionSpeed))
p + geom_point()
p + geom_jitter()
```


```{r}
p <- ggplot(data=train, aes(x=Type, fill=AdoptionSpeed))
p + geom_bar()
```

# Age
The median age is three.
The middle half of the pets are between 2 to 12 years old.

Most pets don't live much past 15 years old. Some pets can life to 20, however, I assume that's not the norm.

17.3% of pets live past age 15.

##RESEARCH QUESTION
Does anyone want to conduct research to determine how to handle pets older than 15?

```{r}
train <-train%>%mutate(Old = ifelse(Age > 15, "Old Age", "Normal Age"))
train$Old <- as.factor(train$Old)
summary(train$Old)
```


```{r}
summary(train$Age)
ggplot(data=train, aes(x=Age)) + 
  geom_histogram(bins = 50)
```

#Gender
There are a lot more females than males. It would be interesting to find out why this is, so we could create additional features.

```{r}
p <- ggplot(data=train, aes(x=Gender))
p + geom_bar()

p <- ggplot(data=train, aes(x=Gender, fill=AdoptionSpeed))
p + geom_bar()

p <- ggplot(data=train, aes(x=Gender, fill=AdoptionSpeed))
p + geom_bar(position="fill")
```

#Color1

```{r}
p <- ggplot(data=train, aes(x=Color1))
p + geom_bar()

p <- ggplot(data=train, aes(x=Color1, fill=AdoptionSpeed))
p + geom_bar()

p <- ggplot(data=train, aes(x=Color1, fill=AdoptionSpeed))
p + geom_bar(position="fill")
```

