276 lines
9.0 KiB
Plaintext
276 lines
9.0 KiB
Plaintext
{
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"name": "xr",
|
|
"display_name": "R 4.4.3 (xr)",
|
|
"language": "R"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": "",
|
|
"file_extension": "R",
|
|
"mimetype": "text/x-R",
|
|
"name": "R",
|
|
"nbconvert_exporter": "",
|
|
"pygments_lexer": "",
|
|
"version": "4.4.3"
|
|
}
|
|
},
|
|
"nbformat_minor": 4,
|
|
"nbformat": 4,
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"source": "# XEUS-R",
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"source": "R.version",
|
|
"metadata": {
|
|
"trusted": true,
|
|
"vscode": {
|
|
"languageId": "r"
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"execution_count": null
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"source": "## Internal packages",
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"source": "library(stats)\n\nset.seed(123)\nx <- rnorm(100)\ny <- 2 * x + rnorm(100)\n\n# Linear regression\nmodel <- lm(y ~ x)\nsummary(model)",
|
|
"metadata": {
|
|
"trusted": true,
|
|
"vscode": {
|
|
"languageId": "r"
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"execution_count": null
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"source": "A <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 3, ncol = 2)\n\n# Singular value decomposition\nsvd_result <- La.svd(A)\n\nprint(svd_result)",
|
|
"metadata": {
|
|
"trusted": true,
|
|
"vscode": {
|
|
"languageId": "r"
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"execution_count": null
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"source": "A <- matrix(c(4, 1, 1, 3), nrow = 2, byrow = TRUE)\n\n# Eigen decomposition\neigen_result <- eigen(A)\n\nprint(eigen_result$values)\n\nprint(eigen_result$vectors)",
|
|
"metadata": {
|
|
"trusted": true,
|
|
"vscode": {
|
|
"languageId": "r"
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"execution_count": null
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"source": "# Example positive-definite matrix\nA <- matrix(c(4, 1, 1, 3), nrow = 2, byrow = TRUE)\n\n# Cholesky decomposition\nchol_result <- chol(A)\n\nprint(chol_result)",
|
|
"metadata": {
|
|
"trusted": true,
|
|
"vscode": {
|
|
"languageId": "r"
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"execution_count": null
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"source": "B <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 3, ncol = 2)\nC <- matrix(c(7, 8, 9, 10, 11, 12), nrow = 3, ncol = 2)\n\n# Perform matrix multiplication using BLAS\nresult <- crossprod(B, C)\n\nprint(result)",
|
|
"metadata": {
|
|
"trusted": true,
|
|
"vscode": {
|
|
"languageId": "r"
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"execution_count": null
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"source": "# Fibonacci sequence\nfibonacci_loop <- function(n) {\n a <- 0\n b <- 1\n\n for (i in 1:n) {\n fib <- a\n cat(fib, \"\\n\")\n a <- b\n b <- fib + b\n }\n return(a)\n}\n\nfibonacci_loop(10)",
|
|
"metadata": {
|
|
"trusted": true,
|
|
"vscode": {
|
|
"languageId": "r"
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"execution_count": null
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"source": "## Plotting",
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"source": "Some of the following R Code was pulled from https://www.statmethods.net/advgraphs/ggplot2.html, and updated to use newer `ggplot2` APIs.",
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"source": "# ggplot2 examples\nlibrary(ggplot2)",
|
|
"metadata": {
|
|
"vscode": {
|
|
"languageId": "r"
|
|
},
|
|
"trusted": true
|
|
},
|
|
"outputs": [],
|
|
"execution_count": null
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"source": "# create factors with value labels\nmtcars$gear <- factor(mtcars$gear,levels=c(3,4,5),\n \tlabels=c(\"3gears\",\"4gears\",\"5gears\"))\nmtcars$am <- factor(mtcars$am,levels=c(0,1),\n \tlabels=c(\"Automatic\",\"Manual\"))\nmtcars$cyl <- factor(mtcars$cyl,levels=c(4,6,8),\n labels=c(\"4cyl\",\"6cyl\",\"8cyl\"))",
|
|
"metadata": {
|
|
"vscode": {
|
|
"languageId": "r"
|
|
},
|
|
"trusted": true
|
|
},
|
|
"outputs": [],
|
|
"execution_count": null
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"source": "# Kernel density plots for mpg\n# grouped by number of gears (indicated by color)\nggplot(mtcars, aes(x = mpg, fill = gear)) +\n geom_density(alpha = 0.5) +\n labs(\n title = \"Distribution of Gas Milage\",\n x = \"Miles Per Gallon\",\n y = \"Density\",\n fill = \"Gears\"\n )",
|
|
"metadata": {
|
|
"vscode": {
|
|
"languageId": "r"
|
|
},
|
|
"trusted": true
|
|
},
|
|
"outputs": [],
|
|
"execution_count": null
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"source": "# Scatterplot of mpg vs. hp for each combination of gears and cylinders\n# in each facet, transmission type is represented by shape and color\nggplot(mtcars, aes(x = hp, y = mpg, shape = am, color = am)) +\n geom_point(size = 3) +\n facet_grid(gear ~ cyl) +\n labs(\n x = \"Horsepower\",\n y = \"Miles per Gallon\",\n shape = \"Transmission\",\n color = \"Transmission\"\n )",
|
|
"metadata": {
|
|
"vscode": {
|
|
"languageId": "r"
|
|
},
|
|
"trusted": true
|
|
},
|
|
"outputs": [],
|
|
"execution_count": null
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"source": "# Separate regressions of mpg on weight for each number of cylinders\nggplot(mtcars, aes(x = wt, y = mpg, color = cyl)) +\n geom_point() +\n geom_smooth(method = \"lm\", formula = y ~ x) +\n labs(\n title = \"Regression of MPG on Weight\",\n x = \"Weight\",\n y = \"Miles per Gallon\",\n color = \"Cylinders\"\n )",
|
|
"metadata": {
|
|
"vscode": {
|
|
"languageId": "r"
|
|
},
|
|
"trusted": true
|
|
},
|
|
"outputs": [],
|
|
"execution_count": null
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"source": "# Boxplots of mpg by number of gears\n# observations (points) are overlayed and jittered\nggplot(mtcars, aes(x = gear, y = mpg, fill = gear)) +\n geom_boxplot() +\n geom_jitter(width = 0.2) +\n labs(\n title = \"Mileage by Gear Number\",\n x = \"\",\n y = \"Miles per Gallon\",\n fill = \"Gears\"\n )",
|
|
"metadata": {
|
|
"vscode": {
|
|
"languageId": "r"
|
|
},
|
|
"trusted": true
|
|
},
|
|
"outputs": [],
|
|
"execution_count": null
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"source": "## Other R Packages",
|
|
"metadata": {
|
|
"vscode": {
|
|
"languageId": "plaintext"
|
|
}
|
|
}
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"source": "library(RColorBrewer)\npalette <- brewer.pal(8, \"Set3\")\nprint(palette)",
|
|
"metadata": {
|
|
"trusted": true,
|
|
"vscode": {
|
|
"languageId": "r"
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"execution_count": null
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"source": "library(crayon)\n\n# Create colorful text\nred_text <- red(\"This is red text\")\ngreen_text <- green(\"This is green text\")\nblue_text <- blue(\"This is blue text\")\nbold_text <- bold(\"This is bold text\")\nunderline_text <- underline(\"This is underlined text\")\n\ncat(red_text, \"\\n\")\ncat(green_text, \"\\n\")\ncat(blue_text, \"\\n\")\ncat(bold_text, \"\\n\")\ncat(underline_text, \"\\n\")",
|
|
"metadata": {
|
|
"trusted": true,
|
|
"vscode": {
|
|
"languageId": "r"
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"execution_count": null
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"source": "library(htmltools)\n\n# Create HMTL elements\ntitle <- tags$title(\"My HTML Page\")\nheader <- tags$h1(\"Hello there!\")\nparagraph <- tags$p(\"This is a paragraph created using htmltools.\")\nlist_items <- tags$ul(tags$li(\"Item 1\"), tags$li(\"Item 2\"), tags$li(\"Item 3\"))\n\n# Combine elements into an HTML document\nhtml_doc <- tags$html(\n tags$head(title),\n tags$body(\n header,\n paragraph,\n list_items\n )\n)\n\nprint(html_doc)",
|
|
"metadata": {
|
|
"trusted": true,
|
|
"vscode": {
|
|
"languageId": "r"
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"execution_count": null
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"source": "library(farver)\n\n# Define a color in RBG space\nrgb_color <- matrix(c(255, 0, 0), ncol = 3)\n\n# Convert RGB to HSV\nhsv_color <- convert_colour(rgb_color, \"rgb\", \"hsv\")\n\n# Convert RGB to LAB\nlab_color <- convert_colour(rgb_color, \"rgb\", \"lab\")\n\nprint(rgb_color)\nprint(hsv_color)\nprint(lab_color)",
|
|
"metadata": {
|
|
"trusted": true,
|
|
"vscode": {
|
|
"languageId": "r"
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"execution_count": null
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"source": "",
|
|
"metadata": {
|
|
"vscode": {
|
|
"languageId": "r"
|
|
},
|
|
"trusted": true
|
|
},
|
|
"outputs": [],
|
|
"execution_count": null
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"source": "",
|
|
"metadata": {
|
|
"trusted": true
|
|
},
|
|
"outputs": [],
|
|
"execution_count": null
|
|
}
|
|
]
|
|
} |