{"cells":[{"metadata":{"_uuid":"d66f3f4a5b2e715ab5ecebb58b163d5598837f1b","_execution_state":"idle","trusted":true},"cell_type":"code","source":"## Importing packages\n\n# This R environment comes with all of CRAN and many other helpful packages preinstalled.\n# You can see which packages are installed by checking out the kaggle/rstats docker image: \n# https://github.com/kaggle/docker-rstats\n\nlibrary(tidyverse) # metapackage with lots of helpful functions\nlibrary(data.table)\nlibrary(plotrix)\nlibrary(ggplot2)\nlibrary(summarytools)\nlibrary(Hmisc)\nlibrary(corrplot)\nlibrary(rgdal)\nlibrary(randomForest)\nlibrary(nnet)\n## Running code\n\n# In a notebook, you can run a single code cell by clicking in the cell and then hitting \n# the blue arrow to the left, or by clicking in the cell and pressing Shift+Enter. In a script, \n# you can run code by highlighting the code you want to run and then clicking the blue arrow\n# at the bottom of this window.\n\n## Reading in files\n\n# You can access files from datasets you've added to this kernel in the \"../input/\" directory.\n# You can see the files added to this kernel by running the code below. \n\nlist.files(path = \"../input\")\n\n## Saving data\n\n# If you save any files or images, these will be put in the \"output\" directory. You \n# can see the output directory by committing and running your kernel (using the \n# Commit & Run button) and then checking out the compiled version of your kernel.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ecafabae8476c15a814a12f3479603ef639debbf"},"cell_type":"code","source":"breeds <- fread('../input/petfinder-adoption-prediction/breed_labels.csv')\n\nglimpse(breeds)\n\nhead(breeds)\nclass(breeds$BreedName)\nlength(unique(breeds$BreedName))\n#There are 307 unique breeds. It's better to keep it as character","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2b998a1e5b404895684fd492f5f75c2e68d79bf4"},"cell_type":"code","source":"colors <- fread('../input/petfinder-adoption-prediction/color_labels.csv')\n\nglimpse(colors)\n\nclrs <- colors$ColorName\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1e401a5c21d68582773c0f47829bf34a15391f1c"},"cell_type":"code","source":"state <- fread('../input/petfinder-adoption-prediction/state_labels.csv')\nglimpse(state)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"898f7d438021beddfe9bc3316dcc391a53e88c94"},"cell_type":"code","source":"\ntrain <- fread(\"../input/petfinder-adoption-prediction/train/train.csv\")\n\nglimpse(train)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"a80d1085fe2429454e2db148bf952f201585331f"},"cell_type":"markdown","source":"Cats & Dogs"},{"metadata":{"trusted":true,"_uuid":"adabebba15bed8e7b977358c24cf142a9b64457b"},"cell_type":"code","source":"\ntrain$Type <- as.factor(train$Type)\n\noptions(repr.plot.width=4, repr.plot.height=3)\nggplot(train, aes(x=Type,y=..count..)) + geom_bar(width=0.5,fill=\"#FF6666\") + scale_x_discrete(breaks = 1:2, labels=c(\"Dog\",\"Cat\"))+ theme_classic()  ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ac062ff17e79bf942299c3453d48f81d0ccec0c8"},"cell_type":"code","source":"\noptions(repr.plot.width=8, repr.plot.height=3)\nggplot(train, aes(x=Age,y=..count..)) + geom_density(fill=\"#FF6666\") +  theme_classic() ","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"12c15960daa314a4fff544030f215a402bb67ed0"},"cell_type":"markdown","source":"Let's zoom in to the left part of the image, as there seem to be outliers"},{"metadata":{"trusted":true,"_uuid":"6ee1331c8c91599b805a092144abfd3101a144d9"},"cell_type":"code","source":"ggplot(train[train$Age<100,], aes(x=Age,y=..count..)) + geom_density(fill=\"#FF6666\") +  theme_classic() ","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"225455355e12ebfbb3cea593fed488832fd9db7c"},"cell_type":"markdown","source":"Let's zoom in more as after 25 there are only bumps at some values"},{"metadata":{"trusted":true,"_uuid":"59558e6d0638282cd7da91705ac1112f5ee649ca"},"cell_type":"code","source":"ggplot(train[train$Age<25,], aes(x=Age,y=..count..)) + geom_density(fill=\"#FF6666\") +  theme_classic() ","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"2270c52c729b0f35903a7974ff9ea7c0c9e3ddef"},"cell_type":"markdown","source":"This gives a little better information. Maximum pets are between age 0-5 months"},{"metadata":{"trusted":true,"_uuid":"a781e06698718841f8309637b241c41bd0370412"},"cell_type":"code","source":"train$PureBreed <- ifelse((train$Breed2==0 & train$Breed1!=0) | (train$Breed1==0 & train$Breed2!=0), 1, 0)\n\ntrain$PureBreed <- as.factor(train$PureBreed)\noptions(repr.plot.width=4, repr.plot.height=3)\nggplot(train, aes(x=PureBreed,y=..count..)) + geom_bar(width=0.5,fill=\"#FF6666\") + scale_x_discrete(breaks = 0:1, labels=c(\"Mixed Breed\",\"Pure Breed\"))+ xlab(\"Breed\")+ theme_classic()  ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c6c27c6f454c6794160423c9b616134f60a2c2c9"},"cell_type":"code","source":"\ntrain$Gender <- as.factor(train$Gender)\noptions(repr.plot.width=4, repr.plot.height=3)\nggplot(train, aes(x=Gender,y=..count..)) + geom_bar(width=0.5,fill=\"#FF6666\") + scale_x_discrete(breaks = 1:3, labels=c(\"Male\",\"Female\", \"Mixed\"))+ xlab(\"Gender\")+ theme_classic()  ","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"02df695441e53090104a9caf4727e9f7641bf174"},"cell_type":"markdown","source":"From this plot we can state 2 things \n1. There are more individual pets as compared to groups\n2. More female pets than male"},{"metadata":{"trusted":true,"_uuid":"206cd6674802eff4f64a9521ba7112c5339ff8ac"},"cell_type":"code","source":"train$MaturitySize <- factor(train$MaturitySize, levels=c(0,1,2,3,4))\ntable(train$MaturitySize)\nggplot(train, aes(x=MaturitySize,y=..count..)) + geom_bar(width=0.5,fill=\"#FF6666\") + scale_x_discrete(breaks = 0:4, labels=c(\"Not Specified\",\"Small\",\"Medium\", \"Large\", \"Extra Large\"))+ xlab(\"Size at maturity\")+ theme_classic()  ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c3b11fff8e9e1c57a1f64b093985df610467a202"},"cell_type":"code","source":"\ntrain$FurLength <- factor(train$FurLength,  levels=c(0,1,2,3))\ntable(train$FurLength)\nggplot(train, aes(x=FurLength,y=..count..)) + geom_bar(width=0.5,fill=\"#FF6666\") + scale_x_discrete(breaks = 0:3, labels=c(\"Not Specified\",\"Small\",\"Medium\", \"Long\"))+ xlab(\"Fur length\")+ theme_classic()  \n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3be25e5445cf0acfcccd3c5df65dad6e684a46a5"},"cell_type":"code","source":"train$Vaccinated <- as.factor(train$Vaccinated)\ntable(train$Vaccinated)\nggplot(train, aes(x=Vaccinated,y=..count..)) + geom_bar(width=0.5,fill=\"#FF6666\") + scale_x_discrete(breaks = 1:3, labels=c(\"Yes\",\"No\",\"Not sure\"))+ xlab(\" Pet has been vaccinated\")+ theme_classic()  \n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"848bcb3d91b4f76eaae708594055fe7fac1844df"},"cell_type":"code","source":"train$Dewormed <- as.factor(train$Dewormed)\ntable(train$Dewormed)\nggplot(train, aes(x=Dewormed,y=..count..)) + geom_bar(width=0.5,fill=\"#FF6666\") + scale_x_discrete(breaks = 1:3, labels=c(\"Yes\",\"No\",\"Not sure\"))+ xlab(\" Pet has been dewormed\")+ theme_classic()  \n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5c83015327ee39d936f6c8d286bcbe4b7a092df9"},"cell_type":"code","source":"train$Sterilized <- as.factor(train$Sterilized)\ntable(train$Sterilized)\nggplot(train, aes(x=Sterilized,y=..count..)) + geom_bar(width=0.5,fill=\"#FF6666\") + scale_x_discrete(breaks = 1:3, labels=c(\"Yes\",\"No\",\"Not sure\"))+ xlab(\"Pet has been spayed / neutered\")+ theme_classic()  \n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f1b2f528f8a0d0205f0b6bb2faca72ef343d2c6a"},"cell_type":"code","source":"train$Health <- factor(train$Health, levels=c(0,1,2,3))\ntable(train$Health)\nggplot(train, aes(x=Health,y=..count..)) + geom_bar(width=0.5,fill=\"#FF6666\") + scale_x_discrete(breaks = 0:3, labels=c(\"Not Specified\",\"Healthy\",\"Minor Injury\",\"Serious Injury\"))+ xlab(\"Health Condition\")+ theme_classic()  \n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"59cda11135e7bc6e25d59d0c4359786935fbbf3d"},"cell_type":"code","source":"train$Quantity <- as.factor(train$Quantity)\noptions(repr.plot.width=8, repr.plot.height=3)\nggplot(train, aes(x=Quantity, y=..count..)) + geom_bar(fill=\"#FF6666\")+ xlab(\"Number of pets represented in profile\")+ theme_classic()  ","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"b1093ac7015d635f3166f3c9a9b4ea6f87b5ad20"},"cell_type":"markdown","source":"Let's create a variable that tells us if the profile respresnts individual or no."},{"metadata":{"trusted":true,"scrolled":true,"_uuid":"cf2a176b7896c87abe14913822d0728033553882"},"cell_type":"code","source":"train$Individual <- ifelse(train$Quantity==1,1,0)\ntrain$Individual <- as.factor(train$Individual)\noptions(repr.plot.width=6, repr.plot.height=3)\nggplot(train, aes(x=Individual, y=..count..)) + geom_bar(width=0.5,fill=\"#FF6666\")+ scale_x_discrete(breaks = 0:1, labels=c(\"Group\",\"Individual\"))+xlab(\"Number of pets represented in profile\")+ theme_classic()  ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":true,"_uuid":"569ef468f083db1191326e44d6db31b37f804d1d"},"cell_type":"code","source":"ggplot(train, aes(x=Fee))+geom_density(fill=\"#FF6666\") +  theme_classic() \n\ndescr(train$Fee,stats = c(\"mean\", \"sd\", \"min\", \"med\", \"max\"), transpose = TRUE, \n      omit.headings = TRUE, style = \"rmarkdown\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8ed6e2c54f015aa013ea205a7db5f723457d5615"},"cell_type":"code","source":"train$AdoptionSpeed <- as.factor(train$AdoptionSpeed)\ntable(train$AdoptionSpeed)\nggplot(train, aes(x=AdoptionSpeed))+geom_bar(fill=\"#FF6666\") + scale_x_discrete(breaks = 0:4, labels=c(\"adopted on the same day\",\"adopted within 1st week\",\"adopted within 1st month\",\"adopted between 2nd & 3rd month\", \"No adoption after 100\"))+ theme_classic() +coord_flip()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8b88d98ba1ca714d4d22e2484581ce60a7137183"},"cell_type":"code","source":"train_stats_by_adoption <- by(data = train, \n                            INDICES = train$AdoptionSpeed, \n                            FUN = descr, stats = c(\"mean\", \"sd\", \"min\", \"med\", \"max\"), \n                            transpose = TRUE)\n\n# Then use view(), like so:\nview(train_stats_by_adoption, method = \"pander\", style = \"rmarkdown\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"751e056625e1a7ee5af49538dd4be91a6d537031"},"cell_type":"code","source":"str(train)\ntrain$AdoptionSpeed <- as.integer(train$AdoptionSpeed)\n\nnums <- unlist(lapply(train, is.numeric))\nunlist(nums)\ntrain[,..nums]\n\nr <- cor(train[, ..nums])\n\n(corrplot(r, type=\"upper\"))\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"7f71590039cbc43b2b6ee44a2328523e5b701ec7"},"cell_type":"markdown","source":"There is no strong correlatiob between numeric variables. Let's understand how categorical variables for relation with each other"},{"metadata":{"_uuid":"8fcd9ede3e462e3e97c5fcd1017bd058b7491ca5"},"cell_type":"markdown","source":"**Bivariate Analysis**"},{"metadata":{"_uuid":"da35fdc50a483500048d4d911d2d69adbb219d9b"},"cell_type":"markdown","source":" **1. Age wise Adoption speed**\n \n As there are few pets with age way higher than the main distribution, we will analyze by removing those rows for now\n We are considering pets with age <=25 months first"},{"metadata":{"trusted":true,"_uuid":"aacb38416771b72cff963adaa0b179f66e6b9e66"},"cell_type":"code","source":"options(repr.plot.width=9, repr.plot.height=4)\n\nggplot(train[train$Age<25,] %>% \n       group_by(AdoptionSpeed) %>%\n       mutate(weight=1/n()), aes(x=Age, fill=factor(AdoptionSpeed))) +geom_bar(aes(weight=weight), stat=\"count\", position=\"dodge\") + \nscale_y_continuous(labels= scales::percent) +scale_fill_brewer(palette=\"Spectral\") + theme_classic() +\ntheme(legend.position=\"top\")+  labs(title=\"Adoption Speed by Age\", ftext=\"Adoption Speed\")","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"c019547e2bdb27062855363648fd70e9426f2a4c"},"cell_type":"markdown","source":"The adoption speed is quickest for pets with Age about 3 months, as the age increases the adoption speed is getting slower."},{"metadata":{"trusted":true,"_uuid":"06d51b2fb63fb9a82583c8b8dbc3ae15cfcd5921"},"cell_type":"code","source":"ggplot(train[train$Age>=25 & train$Age<100,] %>% \n       group_by(AdoptionSpeed) %>%\n       mutate(weight=1/n()), aes(x=Age, fill=factor(AdoptionSpeed))) +geom_bar(aes(weight=weight), stat=\"count\", position=\"dodge\") + \nscale_y_continuous(labels= scales::percent) +scale_fill_brewer(palette=\"Spectral\")+ theme_classic() +\ntheme(legend.position=\"top\") + labs(title=\"Adoption Speed by Age\")","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"12801f114858bb80a6c8860cb4636b2b880c75c9"},"cell_type":"markdown","source":"Pets with age around 35 months have the highest rate of taking longet to get adopted."},{"metadata":{"trusted":true,"_uuid":"c59d383f814ca7775ee17a51f8f7d6f244166519"},"cell_type":"code","source":"ggplot(train[train$Age>100,] %>% \n       group_by(AdoptionSpeed) %>%\n       mutate(weight=1/n()), aes(x=Age, fill=factor(AdoptionSpeed))) +geom_bar(aes(weight=weight), stat=\"count\", position=\"dodge\") + \nscale_y_continuous(labels= scales::percent) +scale_fill_brewer(palette=\"Spectral\")+\ntheme(legend.position=\"Top\")+ theme_classic() + labs(title=\"Adoption Speed by Age\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"548b2cf936c57d0f61281f512a90e94993197b8c"},"cell_type":"code","source":"#MaturitySize\n\nggplot(train %>% \n       group_by(AdoptionSpeed) %>%\n       mutate(weight=1/n()), aes(x=MaturitySize, fill=factor(AdoptionSpeed))) +geom_bar(aes(weight=weight), stat=\"count\", position=\"dodge\") + \nscale_y_continuous(labels= scales::percent) +scale_fill_brewer(palette=\"Spectral\")+\ntheme(legend.position=\"Top\")+ theme_classic() + labs(title=\"Adoption Speed by Maturity Size\") +\nscale_x_discrete(breaks = 0:4, labels=c(\"Not Specified\",\"Small\",\"Medium\", \"Large\", \"Extra Large\"))\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"88c7953040df0e109f56d19896b1de9bd081d5a9"},"cell_type":"markdown","source":"Out of total pets adopted on the same day as listed, 35% Pets are having maturity size Small\nWi Medium are adopted more quickly"},{"metadata":{"trusted":true,"_uuid":"e07072359eb8e9f4b78e1bfb92a10ba2f151a1d2"},"cell_type":"code","source":"#FurLength\n#levels(train$FurLength)\n\n\nggplot(train %>% \n       group_by(AdoptionSpeed) %>%\n       mutate(weight=1/n()), aes(x=FurLength, fill=factor(AdoptionSpeed))) +geom_bar(aes(weight=weight), stat=\"count\", position=\"dodge\") + \nscale_y_continuous(labels= scales::percent) +scale_fill_brewer(palette=\"Spectral\")+\ntheme(legend.position=\"Top\")+ theme_classic() + labs(title=\"Adoption Speed by Fur length\") +\nscale_x_discrete(breaks = 0:3, labels=c(\"Not Specified\",\"Short\",\"Medium\", \"Long\"))\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"ed4cb328450b6db15de467482f27759d5b0bc9b2"},"cell_type":"markdown","source":"There is no clear pattern in FurLength that relates to AdoptionSpeed"},{"metadata":{"trusted":true,"_uuid":"40cda456e93313bde68f77cd73deaba3175bfad2"},"cell_type":"code","source":"#Vaccinated\n\nggplot(train %>% \n       group_by(AdoptionSpeed) %>%\n       mutate(weight=1/n()), aes(x=Vaccinated, fill=factor(AdoptionSpeed))) +geom_bar(aes(weight=weight), stat=\"count\", position=\"dodge\") + \nscale_y_continuous(labels= scales::percent) +scale_fill_brewer(palette=\"Spectral\")+\ntheme(legend.position=\"Top\")+ theme_classic() + labs(title=\"Adoption Speed by Vaccination\") +\nscale_x_discrete(breaks = 1:3, labels=c(\"Yes\",\"No\",\"Not Sure\"))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8fd78910111fb4777d537c8d37ff515e9e7f7e48"},"cell_type":"markdown","source":"Surprisingly non vaccineted "},{"metadata":{"trusted":true,"_uuid":"c77140c82ca492802c5dbdc222cdae11642c5169"},"cell_type":"code","source":"#Dewormed\n\nggplot(train %>% \n       group_by(AdoptionSpeed) %>%\n       mutate(weight=1/n()), aes(x=Dewormed, fill=factor(AdoptionSpeed))) +geom_bar(aes(weight=weight), stat=\"count\", position=\"dodge\") + \nscale_y_continuous(labels= scales::percent) +scale_fill_brewer(palette=\"Spectral\")+\ntheme(legend.position=\"Top\")+ theme_classic() + labs(title=\"Adoption Speed by Dewormed\") +\nscale_x_discrete(breaks = 1:3, labels=c(\"Yes\",\"No\",\"Not Sure\"))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"a65e29d3db3158a68bf833c1a63b628e5af77e2f"},"cell_type":"markdown","source":"Dewormed pets have higher adoption rate than pets that have not been dewormed."},{"metadata":{"trusted":true,"_uuid":"14248d181a94ea24a09de6240c7b31250dce291c"},"cell_type":"code","source":"#Sterilized\n\nggplot(train %>% \n       group_by(AdoptionSpeed) %>%\n       mutate(weight=1/n()), aes(x=Sterilized, fill=factor(AdoptionSpeed))) +geom_bar(aes(weight=weight), stat=\"count\", position=\"dodge\") + \nscale_y_continuous(labels= scales::percent) +scale_fill_brewer(palette=\"Spectral\")+\ntheme(legend.position=\"Top\")+ theme_classic() + labs(title=\"Adoption Speed by Sterilized\") +\nscale_x_discrete(breaks = 1:3, labels=c(\"Yes\",\"No\",\"Not Sure\"))\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"73d37377140c59049be5a652af616ea8dcd5bb92"},"cell_type":"markdown","source":"Adoption speed is highest amongst non seterilized pets"},{"metadata":{"trusted":true,"_uuid":"9c85692be0b99aa57e906acdcecaf4ce6e10e8dc"},"cell_type":"code","source":"#Health\nggplot(train %>% \n       group_by(AdoptionSpeed) %>%\n       mutate(weight=1/n()), aes(x=Health, fill=factor(AdoptionSpeed))) +geom_bar(aes(weight=weight), stat=\"count\", position=\"dodge\") + \nscale_y_continuous(labels= scales::percent) +scale_fill_brewer(palette=\"Spectral\")+\ntheme(legend.position=\"Top\")+ theme_classic() + labs(title=\"Adoption Speed by Health\") +\nscale_x_discrete(breaks = 0:3, labels=c(\"Not Specified\",\"Healthy\",\"Minor Injury\", \"Serious Injury\"))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"6df2202e4b2d0da475cc5cddbafba34fd0f027b3"},"cell_type":"markdown","source":"Above 90% of the pets that are healthy are adopted"},{"metadata":{"trusted":true,"_uuid":"43327168f6e12149bc009612070377b8952fb1e4"},"cell_type":"code","source":"#Quantity \nggplot(train %>% \n       group_by(AdoptionSpeed) %>%\n       mutate(weight=1/n()), aes(x=Quantity, fill=factor(AdoptionSpeed))) +geom_bar(aes(weight=weight), stat=\"count\", position=\"dodge\") + \nscale_y_continuous(labels= scales::percent) +scale_fill_brewer(palette=\"Spectral\")+\ntheme(legend.position=\"Top\")+ theme_classic() + labs(title=\"Adoption Speed by Quantity\")","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"c2743d5dc587c8f2bde066ad05147154237e829e"},"cell_type":"markdown","source":"People prefer to adopt individual pept over group of pets"},{"metadata":{"trusted":true,"_uuid":"072c524905a984eb80eb2d53924383366853387e"},"cell_type":"code","source":"#Fee\n\nggplot(train, aes(x=factor(AdoptionSpeed), y=Fee)) + \n   geom_boxplot(notch = TRUE, fill = \"lightgray\")+\n  stat_summary(fun.y = mean, geom = \"point\",\n               shape = 18, size = 2.5, color = \"#FC4E07\") + theme_classic()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"726d26901aac6e751fb4bfe0fc94843164c7698e"},"cell_type":"markdown","source":"The mean,median of Fees for all adoption speeds are at zero."},{"metadata":{"trusted":true,"_uuid":"9c9d274f2d2a8244e90254a0e1ef6e4a59418612"},"cell_type":"code","source":"\n# Read this shape file with the rgdal library. \n#dsn=\"../input/mapshapefile\"\n#list.files(path = \"../input/mapshapefile\")\n#my_spdf=readOGR(dsn, layer=\"TM_WORLD_BORDERS_SIMPL-0.3\") \n# -- > Now you have a Spdf object (spatial polygon data frame). You can start doing maps!\n\n#library(raster)\n#s <- shapefile(\"../input/mapshapefile/TM_WORLD_BORDERS_SIMPL-0.3.shp\")\n#library(\"highcharter\")\n#hcmap(\"countries/my/my-all\")\n\n#highchart() %>%\n# hc_add_series_map(worldgeojson)\n\n#hcmap(map = \"custom/world\",\n#download_map_data = getOption(\"highcharter.download_map_data\"),\n#data = NULL, value = NULL, joinBy = NULL)\n\n#library(tmap)\n#library(tmaptools)\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e4716a2636576348d4cd0c39bca29559e909d143"},"cell_type":"code","source":"#Video Amount\ntemp_df <- train %>% group_by(AdoptionSpeed) %>% mutate(weight=1/n(), cnt = n())\n#temp_df$weight <- temp_df$weight\nggplot(temp_df, aes(x=VideoAmt,fill=factor(AdoptionSpeed))) +geom_bar(aes(weight=weight),stat=\"count\", position=\"dodge\") + \n#geom_text(aes(label=cnt), position=position_dodge(width = 0.5),vjust=-0.5, check_overlap=TRUE)+\n#geom_text(aes(label=round(weight), y=round(weight)), position=position_dodge(width = 0.5), vjust=-0.5)+\nscale_y_continuous(labels= scales::percent) +scale_fill_brewer(palette=\"Spectral\")+\ntheme(legend.position=\"Top\")+ theme_classic() + labs(title=\"Adoption Speed by Video Amount\")\n#head(temp_df)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"7dc28215df27e5815e88cd1982ef41dd03bf2044"},"cell_type":"markdown","source":"Let's check if the data has NA values"},{"metadata":{"trusted":true,"_uuid":"5e53439058bfc6fca28c722cac6052aa0335d763"},"cell_type":"code","source":"sum(is.na(train))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"7e6352bb1a429b8aa04506d0585e3572d6845f45"},"cell_type":"markdown","source":"Wow! That's dreamy! No NAs.. oh wait, let's also check for blank values"},{"metadata":{"trusted":true,"_uuid":"01f2410457e70702446db78e921abff46eb6c605"},"cell_type":"code","source":"length(which(train ==\"\"))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1f2140ba0d92803e807c711fe5d4f614e47c91a7"},"cell_type":"code","source":"summary(train)\nlength(train[train$Name==\"\"])","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"ad580707a12d8e1e1a69978a3a15394cb790cc7c"},"cell_type":"markdown","source":"26 pets do not have Name. Lets check these rows"},{"metadata":{"trusted":true,"_uuid":"bee3a163125313e3260296fc28e1c5fd2d1c6b0f"},"cell_type":"code","source":"train[train$Name==\"\",]","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d87dd63c66af62449201a87f72fbb013823293dc"},"cell_type":"markdown","source":"We can not replace blank Name. We will not use it as one of the predictors\nWe will use all numeric variables for now to train the mode"},{"metadata":{"trusted":true,"_uuid":"18461bbe85eae3b41ee56e76055afe1916c4fb3d"},"cell_type":"code","source":"train$AdoptionSpeed <- as.factor(train$AdoptionSpeed)\nrf_model <- randomForest(AdoptionSpeed~.-Name-RescuerID-PetID-Description, data=train, ntree=100)\nplot(rf_model)\n\nvarImpPlot(rf_model)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3a132ddab948315c39349df0114648077e8eedd2"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"85da7a0ed8cc6ef2ae0cf31014a391765e450add"},"cell_type":"code","source":"#Try 2 with selective variables\n\nrf_model <- randomForest(AdoptionSpeed~Age+PhotoAmt+Breed1+Color2+Color1+State, data=train, ntree=100)\nplot(rf_model)\n#pred <- predict(rf_model, newdata=xtest)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"de90d5aec9576d3826ac66c1d43ab1674a8a3b63"},"cell_type":"code","source":"test <- fread(\"../input/petfinder-adoption-prediction/test/test.csv\")\n\nglimpse(test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1bc947751ecad57271257611670554ff167f6120"},"cell_type":"code","source":"test$PureBreed <- ifelse((test$Breed2==0 & test$Breed1!=0) | (test$Breed1==0 & test$Breed2!=0), 1, 0)\ntest$PureBreed <- as.factor(test$PureBreed)\n\ntest$Individual <- ifelse(test$Quantity==1,1,0)\ntest$Individual <- as.factor(test$Individual)\nnames(train)\nnames(test)\n\ntest$Type <- as.factor(test$Type)\ntest$PureBreed <- as.factor(test$PureBreed)\ntest$Gender <- as.factor(test$Gender)\ntest$MaturitySize <- factor(test$MaturitySize, levels=c(0,1,2,3,4))\ntest$FurLength <- factor(test$FurLength,, levels=c(0,1,2,3))\ntest$Vaccinated <- as.factor(test$Vaccinated)\ntest$Dewormed <- as.factor(test$Dewormed)\ntest$Sterilized <- as.factor(test$Sterilized)\ntest$Health <- factor(test$Health, levels=c(0,1,2,3))\ntest$Quantity <- as.factor(test$Quantity)\ntest$Individual <- as.factor(test$Individual)\n\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9c19994f0f69ed8cfa8357d500cb6721ed4c6903"},"cell_type":"code","source":"levels(test$Type) <- levels(train$Type)\n\nlevels(test$PureBreed) <- levels(train$PureBreed) \n\nlevels(test$Gender) <- levels(train$Gender)\n\nlevels(test$MaturitySize) <- levels(train$MaturitySize)\n\nlevels(test$FurLength) <- levels(train$FurLength)\n\nlevels(test$Vaccinated) <- levels(train$Vaccinated)\n\nlevels(test$Dewormed) <- levels(train$Dewormed) \n\nlevels(test$Sterilized) <- levels(train$Sterilized)\n\nlevels(test$Health) <- levels(train$Health)\n\ntrain$Quantity  <- as.integer(train$Quantity)\ntest$Quantity  <- as.integer(test$Quantity)\nlevels(test$Individual)  <- levels(train$Individual) \n\nclass(test$Quantity)\n\n\n#pred <- predict(rf_model, newdata=test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c3f4301d157b4e4db89a6adbdea47116e6253f00"},"cell_type":"code","source":"comb <- rbind (train, test, fill=TRUE)\nstr(comb)\ncomb$Type <- as.factor(comb$Type)\ncomb$PureBreed <- as.factor(comb$PureBreed)\ncomb$Gender <- as.factor(comb$Gender)\ncomb$MaturitySize <- factor(comb$MaturitySize, levels=c(0,1,2,3,4))\ncomb$FurLength <- factor(comb$FurLength,, levels=c(0,1,2,3))\ncomb$Vaccinated <- as.factor(comb$Vaccinated)\ncomb$Dewormed <- as.factor(comb$Dewormed)\ncomb$Sterilized <- as.factor(comb$Sterilized)\ncomb$Health <- factor(comb$Health, levels=c(0,1,2,3))\ncomb$Quantity <- as.factor(comb$Quantity)\ncomb$Individual <- as.factor(comb$Individual)\n#comb$AdoptionSpeed <-  fator(comb$AdoptionSpeed , levels=c(0,1,2,3,4))\n\nindextrain <- 1:14993\nxtrain <- comb[indextrain, ]\nxtest <- comb[-indextrain, -24]\n\n\n#rf_model1 <- randomForest(AdoptionSpeed~.-Name-RescuerID-PetID-Description, data=xtrain, ntree=100)\n\n#pred <- predict(rf_model1, newdata=xtest)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b0f0435173408519f93724d174565946dba3a691"},"cell_type":"code","source":"rf_model <- randomForest(AdoptionSpeed~Age+PhotoAmt+Breed1+Color2+Color1+State, data=train, ntree=100)\nplot(rf_model)\npred <- predict(rf_model, newdata=xtest)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5b4ef5893fca7c500ad2da74bfcf77082ec274f1"},"cell_type":"code","source":"head(pred)\n\nsubmission <- data.frame(cbind(xtest$PetID,pred))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7f2d21cb94a2f98eab08779dd0d69d92c05b3742"},"cell_type":"code","source":"\nnames(submission) <- c(\"PetID\", \"AdoptionSpeed\")\nhead(submission)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"050641da06b011551bef81064df40111f4b9958b"},"cell_type":"markdown","source":"Trying logistic regression"},{"metadata":{"trusted":true,"_uuid":"d08284f481313f55e62b1a860794f267aeda0063"},"cell_type":"code","source":"table(xtrain$AdoptionSpeed)\nlevels(xtrain$AdoptionSpeed)\n#nnet_model <- nnet::multinom(AdoptionSpeed~.-Name-RescuerID-PetID-Description, data = xtrain)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d5b00b6c91e7f6535fd0c05e64ad555a3f50df5c"},"cell_type":"code","source":"#summary(nnet_model)\n#xtrain$AdoptionSpeed2 <- relevel(xtrain$AdoptionSpeed, ref = \"1\")\n\n\n#pred <-  predict(nnet_model, newdata=xtest)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4969c1c0012cab17829842c2998992c4137c6cf1"},"cell_type":"code","source":"write.csv( submission,file=\"submission.csv\", row.names=FALSE)","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"R","language":"R","name":"ir"},"language_info":{"mimetype":"text/x-r-source","name":"R","pygments_lexer":"r","version":"3.4.2","file_extension":".r","codemirror_mode":"r"}},"nbformat":4,"nbformat_minor":1}