{"cells":[{"metadata":{"_uuid":"051d70d956493feee0c6d64651c6a088724dca2a","_execution_state":"idle","trusted":true},"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 5GB 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 to /kaggle/temp/, but they won't be saved outside of the current session","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"library(tidyverse)\nlibrary(randomForest)\nlibrary(caret)\n\n\n\n\nbasedir<-path.expand(\"/kaggle/input/osic-pulmonary-fibrosis-progression/\")\ntrd<-read.csv(file.path(basedir,\"train.csv\")) \nted<-read.csv(file.path(basedir,\"test.csv\"))\nsb<-read.csv(file.path(basedir,\"sample_submission.csv\"))\nworkingdir<-file.path(\"../input/output\")\n\n#submission    \ninfos<-trd %>%\n  select(!c(FVC,Percent,Weeks)) %>%\n  unique()\n\nst<-sb %>%\n  separate(Patient_Week, sep=\"_\",c(\"Patient\",\"Weeks\")) %>%\n  mutate(Patient_Week=sb$Patient_Week) %>%\n  merge(infos, by=\"Patient\")\n#test\n\n# ids<-unique(trd$Patient)\nstrf<-st %>%\nselect(!c(Patient_Week,FVC,Confidence))\n\npweek<-preProcess(trd[,-c(3,4)], method = c(\"range\"), rangeBounds = c(0,1))\ntrdp<-cbind(predict(pweek,trd[,-c(3,4)]),FVC=trd$FVC)\ntedp<-cbind(predict(pweek,ted[,-c(3,4)]),FVC=ted$FVC) \n\n\nff<-function(strf,nt){ \n  #j <- 1+ i %% 3\n  # data<-ted[trd$Patient %in% id ,-c(1,3,4)]\n  rr<-randomForest (FVC ~.,data=trdp[,-1], importance=T,ntree=nt,corr.bias=T ,mtry=2)\n  a<-predict(rr,strf)\n  return(a)\n}\nperf<-function(r,testset){\n  sigma<-apply(r,1,sd)\n  FVCp<-apply(r,1,mean)\n  FVC<-testset$FVC\n  sigmac<- sapply(sigma,FUN=function(x) max(x,70))\n  delta<-sapply(FVCp,FUN=function(x)min((FVC-x),1000))\n  metric<-  -sqrt(2)* delta/sigmac - log(sqrt(2)*  sigmac)\n  return(metric)\n}\n\n\ntestset<- st %>%\n  #select(!c(,Confidence,FVC))%>%\n  mutate(Weeks=as.numeric(Weeks)) \ntestset<-predict(pweek,testset) %>%\n  select(!c(FVC,Confidence,Patient_Week,Patient))\n\nr<-sapply(seq(900,1000,50),ff,strf=testset)\n\n\n\nsigma<-apply(r,1,sd)\nFVCp<-apply(r,1,mean)\nresult<-sum(perf(r,tedp))\nsb$FVC<-FVCp\nsb$Confidence<-sigma\n#write.csv(sb,file=file.path(workingdir,\"submission.csv\"),row.names = F)\nwrite.csv(sb,file=\"submission.csv\",row.names = F)\nhead(sb)               ","execution_count":null,"outputs":[]}],"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":"3.6.3"}},"nbformat":4,"nbformat_minor":4}