{"metadata":{"kernelspec":{"name":"ir","display_name":"R","language":"R"},"language_info":{"name":"R","codemirror_mode":"r","pygments_lexer":"r","mimetype":"text/x-r-source","file_extension":".r","version":"4.0.5"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This R environment comes with many helpful analytics packages installed\n# It is defined by the kaggle/rstats Docker image: https://github.com/kaggle/docker-rstats\n# For example, here's a helpful package to load\n\nlibrary(tidyverse) # metapackage of all tidyverse packages\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nlist.files(path = \"../input\")\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files tosion","metadata":{"_uuid":"051d70d956493feee0c6d64651c6a088724dca2a","_execution_state":"idle","execution":{"iopub.status.busy":"2022-07-06T18:30:50.360558Z","iopub.execute_input":"2022-07-06T18:30:50.362108Z","iopub.status.idle":"2022-07-06T18:30:50.382530Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"suppressMessages(library(tidyverse))\ntrain_set <- read_csv(\"/kaggle/input/titanic/train.csv\", col_types = cols())","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:30:50.384787Z","iopub.execute_input":"2022-07-06T18:30:50.386126Z","iopub.status.idle":"2022-07-06T18:30:50.423411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"head(train_set)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:30:50.425777Z","iopub.execute_input":"2022-07-06T18:30:50.427324Z","iopub.status.idle":"2022-07-06T18:30:50.453711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_set <- read_csv(\"/kaggle/input/titanic/test.csv\", col_types = cols())\nhead(test_set)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:30:50.456619Z","iopub.execute_input":"2022-07-06T18:30:50.458595Z","iopub.status.idle":"2022-07-06T18:30:50.502825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gender_submission <- read_csv(\"/kaggle/input/titanic/gender_submission.csv\", col_types = cols())\nhead(gender_submission)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:30:50.505303Z","iopub.execute_input":"2022-07-06T18:30:50.506731Z","iopub.status.idle":"2022-07-06T18:30:50.543548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dim(train_set)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:30:50.546203Z","iopub.execute_input":"2022-07-06T18:30:50.547811Z","iopub.status.idle":"2022-07-06T18:30:50.563580Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"colSums(is.na(train_set))","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:30:50.565971Z","iopub.execute_input":"2022-07-06T18:30:50.567284Z","iopub.status.idle":"2022-07-06T18:30:50.583479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_set <- train_set %>% select(-Cabin)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:30:50.585646Z","iopub.execute_input":"2022-07-06T18:30:50.586940Z","iopub.status.idle":"2022-07-06T18:30:50.602118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"summary(train_set$Age)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:30:50.604449Z","iopub.execute_input":"2022-07-06T18:30:50.605777Z","iopub.status.idle":"2022-07-06T18:30:50.620425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_set %>% ggplot(aes(Age)) + geom_histogram()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:30:50.622560Z","iopub.execute_input":"2022-07-06T18:30:50.623841Z","iopub.status.idle":"2022-07-06T18:30:50.838569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_set$Age <- ifelse(!is.na(train_set$Age), train_set$Age, mean(train_set$Age,na.rm = TRUE))\nsum(is.na(train_set$Age))","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:30:50.840799Z","iopub.execute_input":"2022-07-06T18:30:50.842077Z","iopub.status.idle":"2022-07-06T18:30:50.858962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ggplot(train_set,aes(Sex, fill = factor(Survived))) + geom_bar()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:30:50.861083Z","iopub.execute_input":"2022-07-06T18:30:50.862323Z","iopub.status.idle":"2022-07-06T18:30:51.128764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_set %>% ggplot(aes(PassengerId, Fare, color = factor(Survived))) + geom_point()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:30:51.131089Z","iopub.execute_input":"2022-07-06T18:30:51.132426Z","iopub.status.idle":"2022-07-06T18:30:51.478272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vars <- list(train_set$Pclass, train_set$SibSp, train_set$Parch, train_set$Embarked)\nlapply(vars, unique)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:30:51.480578Z","iopub.execute_input":"2022-07-06T18:30:51.481945Z","iopub.status.idle":"2022-07-06T18:30:51.500968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_set %>% ggplot(aes(Pclass, fill = factor(Survived))) + geom_bar()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:30:51.503322Z","iopub.execute_input":"2022-07-06T18:30:51.504658Z","iopub.status.idle":"2022-07-06T18:30:51.765735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_set %>% ggplot(aes(SibSp, fill = factor(Survived))) + geom_bar()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:30:51.767892Z","iopub.execute_input":"2022-07-06T18:30:51.769202Z","iopub.status.idle":"2022-07-06T18:30:52.034378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_set %>% ggplot(aes(Parch, fill = factor(Survived))) + geom_bar()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:30:52.036570Z","iopub.execute_input":"2022-07-06T18:30:52.037848Z","iopub.status.idle":"2022-07-06T18:30:52.306855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_set %>% ggplot(aes(Embarked, fill = factor(Survived))) + geom_bar()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:30:52.309031Z","iopub.execute_input":"2022-07-06T18:30:52.310330Z","iopub.status.idle":"2022-07-06T18:30:52.567358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_set$Embarked <- ifelse(!is.na(train_set$Embarked), train_set$Embarked, \"S\")\nany(is.na(train_set$Embarked))","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:30:52.569600Z","iopub.execute_input":"2022-07-06T18:30:52.570951Z","iopub.status.idle":"2022-07-06T18:30:52.588162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_set <- train_set %>% select(PassengerId,Pclass,Sex,Age,SibSp,Parch,Fare,Embarked,Survived)\ntest_set <- test_set %>% select(PassengerId, Pclass,Sex,Age,SibSp,Parch,Fare,Embarked)\ntrain_set$Survived <- as.factor(train_set$Survived)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:30:52.590386Z","iopub.execute_input":"2022-07-06T18:30:52.591716Z","iopub.status.idle":"2022-07-06T18:30:52.633329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"suppressMessages(library(caret))\nsuppressMessages(library(randomForest))\n\ngrid <- data.frame(mtry = c(1,2,3,7,13,20))\nrf_model <- train(Survived ~ . , \n                  data = train_set, method = \"rf\",\n                 ntree = 300, tuneGrid = grid,\n                 trControl = trainControl(method = \"repeatedcv\", number = 10, repeats = 3,\n                                         search = \"random\"))","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:30:52.635568Z","iopub.execute_input":"2022-07-06T18:30:52.636870Z","iopub.status.idle":"2022-07-06T18:31:44.028403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(rf_model)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:31:44.030659Z","iopub.execute_input":"2022-07-06T18:31:44.031967Z","iopub.status.idle":"2022-07-06T18:31:44.048537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"colSums(is.na(test_set))","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:31:44.050694Z","iopub.execute_input":"2022-07-06T18:31:44.051965Z","iopub.status.idle":"2022-07-06T18:31:44.067422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_set$Age <- ifelse(!is.na(test_set$Age), test_set$Age, mean(test_set$Age, na.rm = TRUE))\nsum(is.na(test_set$Age))","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:31:44.069653Z","iopub.execute_input":"2022-07-06T18:31:44.070953Z","iopub.status.idle":"2022-07-06T18:31:44.089397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_set %>% ggplot(aes(Fare)) + geom_histogram()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:31:44.091778Z","iopub.execute_input":"2022-07-06T18:31:44.093396Z","iopub.status.idle":"2022-07-06T18:31:44.315380Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"summary(test_set$Fare)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:31:44.317786Z","iopub.execute_input":"2022-07-06T18:31:44.319210Z","iopub.status.idle":"2022-07-06T18:31:44.337234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_set$Fare <- ifelse(!is.na(test_set$Fare), test_set$Fare, median(test_set$Fare, na.rm = TRUE))\nsum(is.na(test_set$Fare))","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:31:44.339728Z","iopub.execute_input":"2022-07-06T18:31:44.341140Z","iopub.status.idle":"2022-07-06T18:31:44.359017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission <- test_set %>% mutate(Survived = predict(rf_model, .))","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:31:44.361350Z","iopub.execute_input":"2022-07-06T18:31:44.362830Z","iopub.status.idle":"2022-07-06T18:31:44.398018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission <- submission %>% select(PassengerId, Survived)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:31:44.407816Z","iopub.execute_input":"2022-07-06T18:31:44.409361Z","iopub.status.idle":"2022-07-06T18:31:44.426154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output <- write.csv(submission, \"submission.csv\", row.names = F)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:31:44.428527Z","iopub.execute_input":"2022-07-06T18:31:44.429911Z","iopub.status.idle":"2022-07-06T18:31:44.444264Z"},"trusted":true},"execution_count":null,"outputs":[]}]}