{"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 to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"051d70d956493feee0c6d64651c6a088724dca2a","_execution_state":"idle","execution":{"iopub.status.busy":"2023-07-01T12:32:45.312767Z","iopub.execute_input":"2023-07-01T12:32:45.315147Z","iopub.status.idle":"2023-07-01T12:32:46.437333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Source\n- https://cran.r-project.org/doc/manuals/r-release/R-intro.html\n- https://cran.r-project.org/doc/manuals/r-release/R-intro.html#Displaying-multivariate-data\n- http://www.sthda.com/english/wiki/scatterplot3d-3d-graphics-r-software-and-data-visualization\n- https://tensorflow.rstudio.com/guides/keras/sequential_model\n- https://www.rdocumentation.org/packages/scater/versions/1.0.4\n- https://www.rdocumentation.org/packages/scater/versions/1.0.4/topics/multiplot\n- https://www.datacamp.com/tutorial/contingency-analysis-r\n- https://www.datacamp.com/tutorial/contingency-tables-r\n- https://rdrr.io/r/\n- https://www.rdocumentation.org/packages/stats/versions/3.6.2/topics/aggregate\n- https://github.com/kassambara/easyGgplot2\n- http://www.sthda.com/english/wiki/ggplot2-multiplot-put-multiple-graphs-on-the-same-page-using-ggplot2\n- https://github.com/lme4/lme4\n- https://cran.r-project.org/web/packages/sjPlot/index.html\n- https://www.r-bloggers.com/2023/06/how-to-map-more-informative-values-onto-fill-argument-of-sjplotplot_model/\n- https://rstudio-pubs-static.s3.amazonaws.com/12641_313407cc39964ec4b55dd9206a815778.html\n- https://www.rensvandeschoot.com/tutorials/lme4/\n- https://www.r-bloggers.com/2023/06/order-constraints-in-bayes-models-with-brms/\n- https://www.r-bloggers.com/2023/06/may-2023-top-40-new-cran-packages/\n- https://bookdown.org/mike/data_analysis/emmeans-package.html\n- https://stackoverflow.com/questions/24387376/r-error-could-not-find-function-multiplot-using-cookbook-example\n","metadata":{}},{"cell_type":"code","source":"library(readr)\ntile_data <- read_csv(\"/kaggle/input/hubmap-hacking-the-human-vasculature/tile_meta.csv\")\nwsi_data <- read_csv(\"/kaggle/input/hubmap-hacking-the-human-vasculature/wsi_meta.csv\")","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2023-07-01T12:32:46.440209Z","iopub.execute_input":"2023-07-01T12:32:46.466764Z","iopub.status.idle":"2023-07-01T12:32:46.707408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Merge the dataframes on the \"name\" column\ndf <- merge(tile_data, wsi_data, on = \"source_wsi\")\n\n# Print the merged dataframe\nhead(df)","metadata":{"execution":{"iopub.status.busy":"2023-07-01T12:32:46.710451Z","iopub.execute_input":"2023-07-01T12:32:46.711469Z","iopub.status.idle":"2023-07-01T12:32:46.766400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"column_names <- colnames(df)\nprint(column_names)","metadata":{"execution":{"iopub.status.busy":"2023-07-01T12:32:46.769204Z","iopub.execute_input":"2023-07-01T12:32:46.770337Z","iopub.status.idle":"2023-07-01T12:32:46.783477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prop.table(table(df$bmi, df$weight))","metadata":{"execution":{"iopub.status.busy":"2023-07-01T12:32:46.786497Z","iopub.execute_input":"2023-07-01T12:32:46.787517Z","iopub.status.idle":"2023-07-01T12:32:46.803527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"chisq.test(df$bmi, df$height)","metadata":{"execution":{"iopub.status.busy":"2023-07-01T12:32:46.806403Z","iopub.execute_input":"2023-07-01T12:32:46.807396Z","iopub.status.idle":"2023-07-01T12:32:46.825726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fisher.test(df$bmi, df$height,simulate.p.value=TRUE)","metadata":{"execution":{"iopub.status.busy":"2023-07-01T12:32:46.828876Z","iopub.execute_input":"2023-07-01T12:32:46.830058Z","iopub.status.idle":"2023-07-01T12:32:46.850684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"library(DescTools)\nGTest(df$age, df$height)","metadata":{"execution":{"iopub.status.busy":"2023-07-01T12:32:46.853566Z","iopub.execute_input":"2023-07-01T12:32:46.854596Z","iopub.status.idle":"2023-07-01T12:32:47.924150Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ftable(df$bmi,df$weight, df$height,df$age)","metadata":{"execution":{"iopub.status.busy":"2023-07-01T12:32:47.926994Z","iopub.execute_input":"2023-07-01T12:32:47.928707Z","iopub.status.idle":"2023-07-01T12:32:47.958191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Goodman and Kruskal lambda is measure based on the proportional reduction in variation.\nLambda(df$bmi,df$weight, direction='row')","metadata":{"execution":{"iopub.status.busy":"2023-07-01T12:32:47.961215Z","iopub.execute_input":"2023-07-01T12:32:47.962834Z","iopub.status.idle":"2023-07-01T12:32:47.990519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"aggregate(weight ~ bmi, data = df, mean)","metadata":{"execution":{"iopub.status.busy":"2023-07-01T12:32:47.993861Z","iopub.execute_input":"2023-07-01T12:32:47.995555Z","iopub.status.idle":"2023-07-01T12:32:48.026573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"summary(df)","metadata":{"execution":{"iopub.status.busy":"2023-07-01T12:32:48.029503Z","iopub.execute_input":"2023-07-01T12:32:48.033172Z","iopub.status.idle":"2023-07-01T12:32:48.090270Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"stem(df$weight)","metadata":{"execution":{"iopub.status.busy":"2023-07-01T12:32:48.099656Z","iopub.execute_input":"2023-07-01T12:32:48.110274Z","iopub.status.idle":"2023-07-01T12:32:48.167340Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sum(complete.cases(df))","metadata":{"execution":{"iopub.status.busy":"2023-07-01T12:32:48.176336Z","iopub.execute_input":"2023-07-01T12:32:48.186706Z","iopub.status.idle":"2023-07-01T12:32:48.216561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Principal Components Analysis\ndf2 <- subset(df, select = -c(i,j,sex, race,id))\n\npca <- prcomp(df2)\nprint(pca)","metadata":{"execution":{"iopub.status.busy":"2023-07-01T12:32:48.221172Z","iopub.execute_input":"2023-07-01T12:32:48.224473Z","iopub.status.idle":"2023-07-01T12:32:48.251816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"matplot(df)","metadata":{"execution":{"iopub.status.busy":"2023-07-01T12:34:10.323447Z","iopub.execute_input":"2023-07-01T12:34:10.324800Z","iopub.status.idle":"2023-07-01T12:34:10.338791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mosaicplot(df2, shade = TRUE, legend = TRUE)","metadata":{"execution":{"iopub.status.busy":"2023-07-01T12:32:49.189315Z","iopub.execute_input":"2023-07-01T12:32:49.192045Z","iopub.status.idle":"2023-07-01T12:32:52.144007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot(df$weight, df$bmi, type = \"p\", col = \"red\")\ntitle(\"Height-Weight\")","metadata":{"execution":{"iopub.status.busy":"2023-07-01T12:32:52.147056Z","iopub.execute_input":"2023-07-01T12:32:52.148722Z","iopub.status.idle":"2023-07-01T12:32:52.317218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"library(scatterplot3d)\nscatterplot3d(df$bmi, df$weight, df$height,pch = 16, color=\"steelblue\")","metadata":{"execution":{"iopub.status.busy":"2023-07-01T12:32:52.320043Z","iopub.execute_input":"2023-07-01T12:32:52.321618Z","iopub.status.idle":"2023-07-01T12:32:52.514875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model <- glm(weight ~ bmi, data = df)\nsummary(model)","metadata":{"execution":{"iopub.status.busy":"2023-07-01T12:32:52.518076Z","iopub.execute_input":"2023-07-01T12:32:52.519536Z","iopub.status.idle":"2023-07-01T12:32:52.547968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# dev.off()\nplot(model)","metadata":{"execution":{"iopub.status.busy":"2023-07-01T12:32:52.550642Z","iopub.execute_input":"2023-07-01T12:32:52.552288Z","iopub.status.idle":"2023-07-01T12:32:53.198150Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Linear model\nlinear_model <- lm(weight ~ bmi + height, data = df)\n\n# Plot the terms\ntermplot(linear_model)","metadata":{"execution":{"iopub.status.busy":"2023-07-01T12:32:53.202231Z","iopub.execute_input":"2023-07-01T12:32:53.203989Z","iopub.status.idle":"2023-07-01T12:32:53.435301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ANOVA table\nanova_table <- aov(model)\nanova_table","metadata":{"execution":{"iopub.status.busy":"2023-07-01T12:32:53.438566Z","iopub.execute_input":"2023-07-01T12:32:53.440196Z","iopub.status.idle":"2023-07-01T12:32:53.462715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"library(graphics)\ncoplot(bmi ~ height | weight, data = df)","metadata":{"execution":{"iopub.status.busy":"2023-07-01T12:32:53.465584Z","iopub.execute_input":"2023-07-01T12:32:53.470835Z","iopub.status.idle":"2023-07-01T12:32:53.703392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"library(stats)\nqqnorm(df$age)","metadata":{"execution":{"iopub.status.busy":"2023-07-01T12:32:53.706439Z","iopub.execute_input":"2023-07-01T12:32:53.708153Z","iopub.status.idle":"2023-07-01T12:32:53.873592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dotchart(df$height)","metadata":{"execution":{"iopub.status.busy":"2023-07-01T12:32:53.876718Z","iopub.execute_input":"2023-07-01T12:32:53.878448Z","iopub.status.idle":"2023-07-01T12:32:55.343791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"library(jsonlite)\nlibrary(dplyr)\n\n# Read the lines of the file into a list\nlines <- readLines(\"/kaggle/input/hubmap-hacking-the-human-vasculature/polygons.jsonl\")\n\n# Convert each line to a JSON object\njson_objects <- lapply(lines, fromJSON)\n\n# Combine the JSON objects into a dataframe\npolygons_df <- bind_rows(json_objects)","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2023-07-01T12:32:55.346810Z","iopub.execute_input":"2023-07-01T12:32:55.348525Z","iopub.status.idle":"2023-07-01T12:33:31.903413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(polygons_df)","metadata":{"execution":{"iopub.status.busy":"2023-07-01T12:33:31.906404Z","iopub.execute_input":"2023-07-01T12:33:31.907614Z","iopub.status.idle":"2023-07-01T12:33:31.942826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"polygons_df$id","metadata":{"execution":{"iopub.status.busy":"2023-07-01T12:33:31.945556Z","iopub.execute_input":"2023-07-01T12:33:31.946575Z","iopub.status.idle":"2023-07-01T12:33:32.096772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"polygons_df.dtypes <- sapply(polygons_df, class)\nprint(polygons_df.dtypes)","metadata":{"execution":{"iopub.status.busy":"2023-07-01T12:33:32.099570Z","iopub.execute_input":"2023-07-01T12:33:32.102073Z","iopub.status.idle":"2023-07-01T12:33:32.114989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"library(raster)\n\n# Read the .tif file\nimage <- raster(\"/kaggle/input/hubmap-hacking-the-human-vasculature/train/00176a88fdb0.tif\")\n\n# Plot the image\nplot(image)\n","metadata":{"execution":{"iopub.status.busy":"2023-07-01T12:33:32.116929Z","iopub.execute_input":"2023-07-01T12:33:32.117921Z","iopub.status.idle":"2023-07-01T12:33:36.907986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"library(tiff)\n\nimage_array = readTIFF(\"/kaggle/input/hubmap-hacking-the-human-vasculature/train/0006ff2aa7cd.tif\")\ndim(image_array)\nhist(image_array[512:3])","metadata":{"execution":{"iopub.status.busy":"2023-07-01T12:33:36.909847Z","iopub.execute_input":"2023-07-01T12:33:36.911173Z","iopub.status.idle":"2023-07-01T12:33:37.004066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"setwd(\"/kaggle/input/hubmap-hacking-the-human-vasculature/train\")\ngetwd()","metadata":{"execution":{"iopub.status.busy":"2023-07-01T12:33:37.006158Z","iopub.execute_input":"2023-07-01T12:33:37.007286Z","iopub.status.idle":"2023-07-01T12:33:37.020195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# library(raster)\n# library(dplyr)\n\n# tif_files <- list.files(path = \"/kaggle/input/hubmap-hacking-the-human-vasculature/train\", pattern = \"*.tif\")\n\n# raster_list <- list()\n\n# for (tif_file in tif_files) {\n#   raster_list[[tif_file]] <- raster(tif_file)\n# }\n\n# # Create a dataframe with the raster data\n# df <- do.call(rbind, raster_list)\n\n","metadata":{"execution":{"iopub.status.busy":"2023-07-01T12:33:37.022003Z","iopub.execute_input":"2023-07-01T12:33:37.022944Z","iopub.status.idle":"2023-07-01T12:33:37.031109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"library(nnet)\n\nmultinom_model <- multinom(weight ~ bmi, data = df)\nsummary(multinom_model)\n\nsummary(multinom_model)","metadata":{"execution":{"iopub.status.busy":"2023-07-01T12:33:37.032979Z","iopub.execute_input":"2023-07-01T12:33:37.033974Z","iopub.status.idle":"2023-07-01T12:33:37.211486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"terms(multinom_model)","metadata":{"execution":{"iopub.status.busy":"2023-07-01T12:33:37.213353Z","iopub.execute_input":"2023-07-01T12:33:37.214290Z","iopub.status.idle":"2023-07-01T12:33:37.224833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# install.packages(\"devtools\")\n# library(devtools)\n# install_github(\"kassambara/easyGgplot2\")","metadata":{"execution":{"iopub.status.busy":"2023-07-01T12:33:37.226655Z","iopub.execute_input":"2023-07-01T12:33:37.227617Z","iopub.status.idle":"2023-07-01T12:33:37.235104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# library(ggplot2)\n# library(gridExtra)\n\n# p1 <- df %>% ggplot() +\n#   geom_point(aes(x = bmi, y = weight))\n# p2 <- df %>% ggplot() +\n#   geom_point(aes(x = height, y = weight))\n\n# multiplot(p1, p2, cols = 2)","metadata":{"execution":{"iopub.status.busy":"2023-07-01T12:33:37.236862Z","iopub.execute_input":"2023-07-01T12:33:37.237812Z","iopub.status.idle":"2023-07-01T12:33:37.245401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# devtools::install_github(\"strengejacke/sjPlot\")\n# remotes::install_github(\"rvlenth/emmeans\")\n# remotes::install_github(\"rvlenth/emmeans\", dependencies = TRUE, build_opts = \"\")\n# install.packages(\"sjPlot\")","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2023-07-01T12:33:37.247193Z","iopub.execute_input":"2023-07-01T12:33:37.248147Z","iopub.status.idle":"2023-07-01T12:33:37.255551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"library(lme4)\n# library(sjPlot)\ndepends <- c(\"MASS\", \"car\", \"foreign\", \"ggplot2\", \"plyr\", \"lmtest\", \"reshape2\", \n    \"scales\", \"quantreg\")\nfor (i in 1:length(depends)) {\n    if ((depends[i] %in% rownames(installed.packages())) == FALSE) {\n        cat(paste0(\"Installing missing package \\\"\", depends[i], \"\\\"...\\n\"))\n        install.packages(depends[i])\n    } else {\n        cat(paste0(\"Package \\\"\", depends[i], \"\\\" already installed...\\n\"))\n    }\n}","metadata":{"execution":{"iopub.status.busy":"2023-07-01T12:33:37.257345Z","iopub.execute_input":"2023-07-01T12:33:37.258301Z","iopub.status.idle":"2023-07-01T12:33:40.996778Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# help(lme4)","metadata":{"execution":{"iopub.status.busy":"2023-07-01T12:33:40.998577Z","iopub.execute_input":"2023-07-01T12:33:40.999876Z","iopub.status.idle":"2023-07-01T12:33:41.007497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Linear mixed effect Model\nlibrary(lme4)\nlibrary(arm)\nln_model <- lmer(bmi ~ weight + (height | age), data = df)\ndisplay(ln_model)","metadata":{"execution":{"iopub.status.busy":"2023-07-01T12:33:41.009296Z","iopub.execute_input":"2023-07-01T12:33:41.010258Z","iopub.status.idle":"2023-07-01T12:33:41.269174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"library(gam)\n\ngam_model <- gam(bmi ~ s(weight) + s(height), data = df)\nsummary(gam_model)","metadata":{"execution":{"iopub.status.busy":"2023-07-01T12:33:41.271047Z","iopub.execute_input":"2023-07-01T12:33:41.272233Z","iopub.status.idle":"2023-07-01T12:33:41.362228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# library(devtools)\n# devtools::install_github(\"strengejacke/sjPlot\")","metadata":{"execution":{"iopub.status.busy":"2023-07-01T12:33:41.364035Z","iopub.execute_input":"2023-07-01T12:33:41.365066Z","iopub.status.idle":"2023-07-01T12:33:41.372729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}