<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>emre şahin's digital garden 🍃 - r</title>
    <link>https://emresahin.net/tags/r/</link>
    <description>Posts in the r tag</description>
    <language>en</language>
    <managingEditor>contact@emresahin.net (Emre Şahin)</managingEditor>
    <lastBuildDate>Tue, 15 Sep 2026 19:46:32 +0000</lastBuildDate>
    <atom:link href="https://emresahin.net/tags/r/rss.xml" rel="self" type="application/rss+xml"/>
    <item>
      <title>ggplot2: Elegant Graphics for Data Analysis</title>
      <published>2013-12-31T22:00:00+00:00</published>
      <updated>2013-12-31T22:00:00+00:00</updated>
      <author>Emre Şahin</author>
      <pubDate>Tue, 31 Dec 2013 22:00:00 +0000</pubDate>
      <link>https://emresahin.net/ggplot2-elegant-graphics/</link>
      <guid isPermaLink="true">https://emresahin.net/ggplot2-elegant-graphics/</guid>
      <description>The important parts of the book are: The grammar of graphics in ggplot2 qplot for easy plotting Geoms (Geometric objects) Linear models in plots qplot() is designed after the base plot() function. The three most important parameters to qplot are x , y , and data . If data is specified, it’s used ...</description>
      <category>R</category>
      <category>Data Science</category>
      <category>Book Notes</category>
      <category>R</category>
      <category>ggplot2</category>
      <category>data-visualization</category>
      <category>data-analysis</category>
      <category>statistics</category>
      <content:encoded><![CDATA[<h1 id="the-important-parts-of-the-book-are">The important parts of the book are:</h1>
<ul>
<li>The grammar of graphics in <code>ggplot2</code></li>
<li><code>qplot</code> for easy plotting</li>
<li>Geoms (Geometric objects)</li>
<li>Linear models in plots</li>
</ul>
<p><code>qplot()</code> is designed after the base <code>plot()</code> function.</p>
<p>The three most important parameters to <code>qplot</code> are <code>x</code>, <code>y</code>, and <code>data</code>.
If <code>data</code> is specified, it’s used as a namespace for variables.</p>
<pre><code class="language-r">qplot(carat, price, data = diamonds)
qplot(carat, x * y * z, data = diamonds)
</code></pre>
<p><code>color</code> is another argument that can be specified for differentiation, as are <code>shape</code> and <code>size</code>.</p>
<p>It’s possible to specify these arguments directly using <code>I()</code>, like <code>I('red')</code>.</p>
<p>There are various <code>geom</code> parameters:</p>
<ul>
<li><code>"point"</code> is the standard scatterplot.</li>
<li><code>"smooth"</code> fits a smoother to the data and shows the standard error as well.</li>
<li><code>"boxplot"</code> produces a box-and-whisker plot.</li>
<li><code>"path"</code> and <code>"line"</code> are used to draw lines between data points.</li>
</ul>
<h2 id="smoothing">Smoothing</h2>
<p>A smoother is specified via the <code>geom = c("point", "smooth")</code> parameter given to <code>qplot</code>. A particular smoother can be specified using the <code>method</code> parameter.</p>
<pre><code class="language-r">qplot(carat, data = diamonds, geom = "histogram")
qplot(carat, data = diamonds, geom = "density")
</code></pre>
<p>These show the distribution of a single variable.</p>
<p>The <code>binwidth</code> parameter can be specified for histograms.</p>
<p>It’s possible to specify subgroup aesthetics using <code>color</code> or <code>fill</code> parameters.</p>
<h2 id="bar-charts">Bar Charts</h2>
<p>Bar charts count the number of instances per class. There is no need to specify bin sizes in bar charts because bins equal classes.</p>
<h2 id="faceting">Faceting</h2>
<p>Faceting divides the data into subsets and displays the same graph for each subset. To facet, use the <code>facets</code> parameter, like <code>facets = color ~ size</code> or, when a single parameter is needed, <code>facets = color ~ .</code>.</p>
<h1 id="q-how-can-i-add-legends">Q: How can I add legends?</h1>
<p>In <code>ggplot2</code>, legends are produced automatically by the geoms and scales. There is little you can do to directly control the legends.</p>
<h1 id="q-how-can-i-use-faceting-in-ggplot">Q: How can I use faceting in ggplot?</h1>
<p>The following example is for <code>qplot</code>:</p>
<pre><code class="language-r">qplot(cty, hwy, data = mpg2) + facet_grid(. ~ .)
</code></pre>
<p>The same function <code>facet_grid</code> can be used in <code>ggplot</code> as well.</p>
<h1 id="q-how-can-i-use-linear-regression">Q: How can I use linear regression?</h1>
<pre><code class="language-r">qplot(carat, price, data = diamonds, log = "xy") +
  geom_smooth(method = "lm")
</code></pre>
<h1 id="q-how-can-i-use-multiple-sources-of-data-frames">Q: How can I use multiple sources of data frames?</h1>
<p>The data should be a <code>data.frame</code>. <code>ggplot2</code> cannot read from multiple sources directly. However, it’s possible to use multiple layers with different datasets by specifying different data for each <code>geom_</code> function.</p>
<h1 id="q-how-generic-is-ggplot">Q: How generic is ggplot?</h1>
<p>The grammar is generic enough, but the <code>data.frame</code> restriction on <code>ggplot</code> is important.</p>
<h1 id="q-how-are-mappings-between-data-frames-and-plot-values-determined">Q: How are mappings between data frames and plot values determined?</h1>
<p>The <code>aes</code> function is used to map data frame columns to layers in the plot. For different <code>geom_</code> functions, different <code>aes</code> parameters are available.</p>
<h1 id="q-what-is-the-difference-between-setting-aes-in-geom_-and-ggplot-functions">Q: What is the difference between setting aes in geom_ and ggplot functions?</h1>
<p>The difference lies in setting the defaults. If mappings do not change for each layer, setting them in the <code>ggplot</code> function saves a few keystrokes. Otherwise, each layer can have its own particular mappings in <code>geom_</code> functions.</p>
<h1 id="q-how-are-different-coordinate-systems-used">Q: How are different coordinate systems used?</h1>
<p>There are <code>coord_</code> functions like <code>coord_cartesian</code> to specify different coordinate systems. <code>coord_trans</code>, <code>coord_map</code>, and <code>coord_polar</code> are some of the most important of these functions.</p>
<h1 id="q-what-else-can-i-learn-from-the-book">Q: What else can I learn from the book?</h1>
<p>Layering strategy, plot annotations, themes, reducing duplicate code, <code>qplot</code> and <code>ggplot</code> conversions, aesthetic specifications, and the grid system.</p>
<h1 id="q-what-is-grouping">Q: What is grouping?</h1>
<p>When <code>x</code> and <code>y</code> are continuous in <code>aes</code>, we may need a <code>group</code> parameter to group these numbers. This should be a discrete variable used to identify different classes of data, such as the height of a particular individual over time.</p>
<h1 id="q-what-is-a-stat">Q: What is a Stat?</h1>
<p>Stats are calculations on a dataset that produce <em>plottable</em> datasets. They can also be referenced in the <code>aes</code> function. Their parameters should be surrounded by dots, like <code>..density..</code>, when referring to them so as not to mistake them for variables in the original dataset.</p>
<h1 id="q-how-do-i-draw-maps">Q: How do I draw maps?</h1>
<p>There is a <code>maps</code> package that provides map data; it is then possible to use these maps in data frames and draw them with <code>geom_polygon</code>, etc.</p>
<h1 id="q-how-do-i-summarize-data">Q: How do I summarize data?</h1>
<p>There is a <code>stat_summary</code> function to summarize data. It has some predefined alternatives and is able to receive simple aggregate functions (like <code>max</code> or <code>mean</code>). It can also receive more complex functions.</p>
<h1 id="q-how-do-i-set-a-theme">Q: How do I set a theme?</h1>
<p>It’s possible to set a theme globally using <code>theme_set</code> and locally by passing a theme function (like <code>theme_grey</code> or <code>theme_bw</code>) to the <code>ggplot</code> function.</p>
<h1 id="q-how-do-i-generate-subplots">Q: How do I generate subplots?</h1>
<p>Subplots are small plots inside larger plots. They can be used to summarize data in another way. It is possible to set viewports using the <code>viewport</code> function and draw another plot within it.</p>
<h1 id="q-what-does-plyr-do-why-is-it-used-here">Q: What does plyr do? Why is it used here?</h1>
<p>The <code>plyr</code> package is used to group and manipulate data frames. It can be used to create any transformation on subsets of data.</p>
<h1 id="q-how-do-i-reduce-duplicate-code">Q: How do I reduce duplicate code?</h1>
<p>The <code>last_plot</code> function returns the last plot created. You can also define groups of plot elements to reuse. <code>qplot</code> also uses these tricks to generate <code>ggplot</code> objects.</p>]]></content:encoded>
    </item>
    <item>
      <title>R Notes</title>
      <published>2013-03-12T14:00:00+00:00</published>
      <updated>2013-03-12T14:00:00+00:00</updated>
      <author>Emre Şahin</author>
      <pubDate>Tue, 12 Mar 2013 14:00:00 +0000</pubDate>
      <link>https://emresahin.net/r-notes/</link>
      <guid isPermaLink="true">https://emresahin.net/r-notes/</guid>
      <description>These are notes I took from various sources, including the Coursera Data Analysis course and R’s online help via help.start() . Basics R objects have attributes which can be observed using the attributes() function. &lt;- is the assignment operator. : is used to create integer sequences. For example...</description>
      <category>Data Science</category>
      <category>Programming</category>
      <category>r</category>
      <category>statistics</category>
      <category>data-analysis</category>
      <category>notes</category>
      <content:encoded><![CDATA[<p>These are notes I took from various sources, including the <a href="https://class.coursera.org/compdata-002/">Coursera Data Analysis</a> course and R’s online help via <code>help.start()</code>.</p>
<h1 id="basics">Basics</h1>
<p>R objects have attributes which can be observed using the <code>attributes()</code> function.</p>
<p><code>&lt;-</code> is the assignment operator.</p>
<p><code>:</code> is used to create integer sequences. For example, <code>1:4</code> results in <code>1 2 3 4</code>.</p>
<p>The <code>c()</code> function (concatenate) can be used to create vectors from different kinds of objects:</p>
<ul>
<li><code>c(TRUE, FALSE)</code> creates a logical vector.</li>
<li><code>c(1+3i, 4+8i, 3-5i)</code> creates a complex vector.</li>
</ul>
<p>Type coercion happens if different kinds of objects are mixed.</p>
<p><code>as.*</code> functions can be used to coerce data types. For example, <code>as.numeric(TRUE)</code> returns <code>1</code>.</p>
<p><code>matrix(ncol = 3, nrow = 4)</code> creates a matrix.</p>
<p><code>cbind()</code> and <code>rbind()</code> are other options to create a matrix from vectors by binding them as columns or rows.</p>
<p><code>factor</code>s are categorized data, like <code>male/female</code>. They are created using the <code>factor()</code> function. The <code>table()</code> and <code>unclass()</code> functions can also be used to get information or change the factor into a numeric table.</p>
<p>The <code>levels</code> parameter in the <code>factor()</code> function can be used to determine the factor-to-number correspondence.</p>
<p><code>is.nan()</code> and <code>is.na()</code> functions are used to check whether vector values are <code>NaN</code> or <code>NA</code>.</p>
<p><code>data.frame</code>s are used to store tabular data like matrices. Unlike matrices, they can store different types of data in each column (e.g., the first column can be numeric, the second a factor, and the third logical).</p>
<p><code>data.frame</code>s are usually created using the <code>read.table()</code> or <code>read.csv()</code> functions. Each row has a name which can be accessed by <code>row.names()</code>. A data frame can be converted to a matrix with <code>data.matrix()</code>.</p>
<p><code>nrow()</code> and <code>ncol()</code> functions can be used to get the number of rows and columns.</p>
<p><code>str()</code> and <code>summary()</code> functions provide concise information about a data structure.</p>
<p>Use <code>getwd()</code> to report the current working directory, and <code>setwd()</code> to change it.</p>
<p>The <code>ls()</code> function displays the names of objects in your workspace:</p>
<pre><code class="language-r">&gt; x &lt;- 10
&gt; y &lt;- 50
&gt; z &lt;- c("three", "blind", "mice")
&gt; f &lt;- function(n, p) sqrt(p * (1 - p) / n)
&gt; ls()
[1] "f" "x" "y" "z"
</code></pre>
<p>The <code>rm()</code> function permanently removes one or more objects from the workspace.</p>
<h1 id="scripts">Scripts</h1>
<p>R executes the <code>.Rprofile</code> script when it starts. The location of <code>.Rprofile</code> depends on your platform; on Linux/Unix, it is typically in your home directory: <code>~/.Rprofile</code>.</p>
<p>The <code>source()</code> function instructs R to read a text file and execute its contents:</p>
<pre><code class="language-r">source("myScript.R")
</code></pre>
<p>On the command line, this can be run as:</p>
<pre><code class="language-bash">$ R CMD BATCH /home/jim/psych/adoldrug/partyuse1.R
</code></pre>
<p>Managing various objects used in R can be challenging. Sorting objects into sensible directory structures can help. You may wish to keep a directory of R scripts that change the working directory to suit the task they perform.</p>
<p><code>par(ask=TRUE)</code> requires you to hit Enter before each plot is displayed.</p>
<p><code>readline("Press &lt;Enter&gt; to continue")</code> presents a prompt to the user.</p>
<h1 id="vectors">Vectors</h1>
<p>Vectors are created like <code>v &lt;- c(1.1, 2.2, 3.3)</code>. Vectors can be used in arithmetic expressions, such as <code>x &lt;- v + 2 * w</code>. A shorter vector is <em>cycled</em> until it reaches the length of the longer vector in arithmetic expressions.</p>
<p><code>range()</code> returns the minimum and maximum elements of a vector.</p>
<p><code>sort()</code> sorts a vector in increasing order.</p>
<p><code>sqrt(-17)</code> returns <code>NaN</code>, but <code>sqrt(-17+0i)</code> returns a complex result.</p>
<p>Regular sequences are generated by the <code>:</code> operator. <code>4:10</code> returns <code>[4, 5, 6, 7, 8, 9, 10]</code>. This is syntactic sugar for the <code>seq()</code> function, which can also specify step size and length.</p>
<p>The <code>rep()</code> function repeats supplied elements to create a vector.</p>
<h1 id="arrays">Arrays</h1>
<p>If <code>z</code> is a vector with 1500 elements (e.g., <code>z &lt;- 1:1500</code>), then <code>dim(z) &lt;- c(3, 5, 100)</code> makes it a 3D array with those dimensions.</p>
<p>Another way to create an array is <code>x &lt;- array(1:20, dim=c(4, 5))</code>.</p>
<h1 id="matrices">Matrices</h1>
<p>Two matrices <code>A</code> and <code>B</code> can be multiplied using <code>A %*% B</code>.</p>
<p>A linear equation of the form <code>b &lt;- A %*% x</code> can be solved using <code>solve(A, b)</code>.</p>
<h1 id="lists">Lists</h1>
<p>A list can be created using the <code>list()</code> function. List elements don’t have to be of the same type; they can be anything from characters to vectors.</p>
<pre><code class="language-r">&gt; mylist &lt;- list(name="Fred", no.children=3, child.ages=c(4, 7, 9))
</code></pre>
<p>Components can be accessed by index like <code>mylist[[1]]</code> or by component name like <code>mylist$no.children</code> or <code>mylist[["no.children"]]</code>.</p>
<p>Lists are similar to structs in other languages. The <code>c()</code> function can be used to concatenate lists.</p>
<h1 id="arbitrary-functions">Arbitrary Functions</h1>
<p>An arbitrary function (similar to a lambda) can be created as <code>f &lt;- function(x, y) x + y</code>.</p>
<h1 id="statistical-functions-for-discrete-distributions">Statistical Functions for Discrete Distributions</h1>
<p>The <code>library(distrEx)</code> provides functions <code>E()</code>, <code>var()</code>, and <code>sd()</code> to calculate mean, variance, and standard deviation.</p>
<p>Uniform random events can be emulated with the <code>sample()</code> function. It has three parameters:</p>
<ol>
<li>The range of values to select from.</li>
<li><code>size</code>: the number of events.</li>
<li><code>replace</code>: whether to sample with replacement.</li>
</ol>
<p>Examples:</p>
<ul>
<li>1000 dice rolls: <code>sample(6, size=1000, replace=TRUE)</code></li>
<li>50 random numbers from 1000 to 2000: <code>sample(1000:2000, size=50, replace=TRUE)</code></li>
<li>Flip a fair coin 100 times: <code>sample(c("H", "T"), size=100, replace=TRUE)</code></li>
</ul>
<h1 id="reading-and-writing-data">Reading and Writing Data</h1>
<p><code>read.table()</code> and <code>read.csv()</code> read tabular data from text files.</p>
<p><code>readLines()</code> reads lines of text.</p>
<p><code>source()</code> and <code>dget()</code> read R code files.</p>
<p><code>load()</code> and <code>unserialize()</code> are used to read binary objects.</p>
<p><code>dump()</code> and <code>dput()</code> are the inverses of <code>source()</code> and <code>dget()</code>. They include the object’s metadata in the output.</p>
<p><code>file()</code> is used to open file connections. <code>gzfile()</code> opens gzipped files, and <code>bzfile()</code> opens bzip2 files. The <code>url()</code> command opens a connection to a web page.</p>
<h2 id="readtable"><code>read.table</code></h2>
<p><code>read.table()</code> is the primary function for importing data.</p>
<ul>
<li><code>file</code>: name of the file or connection.</li>
<li><code>header</code>: boolean indicating if the file has a header row.</li>
<li><code>sep</code>: the field separator (comma, tab, etc.).</li>
<li><code>colClasses</code>: a vector of column data types. Specifying this can make R significantly faster.</li>
<li><code>nrows</code>: the number of rows in the dataset.</li>
<li><code>comment.char</code>: character that starts a comment.</li>
<li><code>skip</code>: number of lines to skip from the beginning.</li>
</ul>
<p><code>read.csv()</code> is a wrapper for <code>read.table()</code> with the default separator set to a comma.</p>
<pre><code class="language-r">initial &lt;- read.table("datatable.txt", nrows = 100)
classes &lt;- sapply(initial, class)
tabAll &lt;- read.table("datatable.txt", colClasses = classes)
</code></pre>
<h1 id="plotting">Plotting</h1>
<p><code>plot(x, y)</code> plots the values in <code>x</code> against <code>y</code>. Additional parameters can configure visual settings.</p>
<p>Use the <code>density()</code> function to approximate sample density, and <code>lines()</code> to draw it:</p>
<pre><code class="language-r">hist(x, prob=T)
lines(density(x))
</code></pre>
<h1 id="installing-r-packages">Installing R packages</h1>
<h2 id="method-1-install-from-source">Method 1: Install from source</h2>
<p>Download the package (e.g., <code>mypkg</code>) and run this in the shell:</p>
<pre><code class="language-bash">$ R CMD INSTALL mypkg -l /my/own/R-packages/
</code></pre>
<h2 id="method-2-install-from-cran">Method 2: Install from CRAN</h2>
<p>Run this in the R console:</p>
<pre><code class="language-r">&gt; install.packages("mypkg", lib="/my/own/R-packages/")
</code></pre>
<h2 id="load-the-library">Load the library</h2>
<pre><code class="language-r">&gt; library("mypkg", lib.loc="/my/own/R-packages/")
</code></pre>
<h1 id="statistics">Statistics</h1>
<h2 id="density">Density</h2>
<p>Approximate sample density and draw it:</p>
<pre><code class="language-r">&gt; hist(x, prob=T)
&gt; lines(density(x))
</code></pre>
<h2 id="-notation-for-relations-between-variables"><code>~</code> notation for relations between variables</h2>
<p>R uses a special notation for describing relationships between variables. Suppose you assume a linear model for a variable $y$, predicted from variables $x_1, x_2, \dots, x_n$:</p>
<p>$y \sim x_1 + x_2 + \dots + x_n$</p>
<p>Statisticians refer to $y$ as the dependent variable and $x_i$ as the independent variables. In R, this is represented as a formula object.</p>
<h1 id="working-with-data">Working with data</h1>
<h2 id="creating-a-data-frame">Creating a Data Frame</h2>
<pre><code class="language-r">&gt; points &lt;- data.frame(label=c("Low", "Mid", "High"),
                       lbound=c(0, 0.67, 1.64),
                       ubound=c(0.674, 1.64, 2.33))
</code></pre>
<h2 id="the-print-function">The <code>print()</code> function</h2>
<p>Allows you to specify the number of printed digits:</p>
<pre><code class="language-r">&gt; print(pi, digits=4)
</code></pre>
<h2 id="the-cat-function">The <code>cat()</code> function</h2>
<p>Does not provide direct control over formatting. Use <code>format()</code> before calling <code>cat()</code>:</p>
<pre><code class="language-r">&gt; cat(format(pi, digits=4), "\n")
</code></pre>
<h2 id="the-listfiles-function">The <code>list.files()</code> function</h2>
<p>Shows the contents of your working directory.</p>
<h2 id="the-writecsv-function">The <code>write.csv()</code> function</h2>
<pre><code class="language-r">&gt; write.csv(x, file="filename.csv", row.names=FALSE)
</code></pre>
<h2 id="factor-analysis">Factor analysis</h2>
<p>Available via <code>factanal()</code> in the <code>stats</code> package:</p>
<pre><code class="language-r">factanal(x, factors, data = NULL, covmat = NULL, n.obs = NA,
          subset, na.action, start = NULL,
          scores = c("none", "regression", "Bartlett"),
          rotation = "varimax", control = NULL, ...)
</code></pre>
<h2 id="pca">PCA</h2>
<p>Principal Components Analysis (PCA) breaks a set of correlated variables into uncorrelated variables. Available via <code>prcomp()</code>.</p>
<h2 id="distributions-in-r">Distributions in R</h2>
<ul>
<li><em>Binomial</em>: <code>binom</code> (n = trials, p = probability)</li>
<li><em>Geometric</em>: <code>geom</code> (p = probability)</li>
<li><em>Hypergeometric</em>: <code>hyper</code> (m = white balls, n = black balls, k = balls drawn)</li>
<li><em>Negative Binomial</em>: <code>nbinom</code></li>
<li><em>Poisson</em>: <code>pois</code> (lambda = mean)</li>
<li><em>Beta</em>: <code>beta</code></li>
<li><em>Cauchy</em>: <code>cauchy</code></li>
<li><em>Chi-squared</em>: <code>chisq</code> (df = degrees of freedom)</li>
<li><em>Exponential</em>: <code>exp</code></li>
<li><em>F</em>: <code>f</code></li>
<li><em>Gamma</em>: <code>gamma</code></li>
<li><em>Log-normal</em>: <code>lnorm</code></li>
<li><em>Logistic</em>: <code>logis</code></li>
<li><em>Normal</em>: <code>norm</code></li>
<li><em>Student’s t</em>: <code>t</code></li>
<li><em>Uniform</em>: <code>unif</code></li>
<li><em>Weibull</em>: <code>weibull</code></li>
<li><em>Wilcoxon</em>: <code>wilcox</code></li>
</ul>
<h2 id="combination-calculation">Combination calculation</h2>
<p>Calculating combinations (n choose k) is done via the <code>choose()</code> function:</p>
<pre><code class="language-r">&gt; choose(5, 3)
[1] 10
</code></pre>
<h2 id="generating-combinations">Generating combinations</h2>
<p>Use the <code>combn()</code> function to generate all combinations:</p>
<pre><code class="language-r">&gt; combn(items, k)
</code></pre>
<h2 id="selecting-n-items-from-a-vector">Selecting $n$ items from a vector</h2>
<pre><code class="language-r">&gt; sample(vec, n)
</code></pre>
<h2 id="dotplot-in-lattice"><code>dotplot()</code> in lattice</h2>
<p>The <code>dotplot()</code> function in <code>library(lattice)</code> is useful for displaying labeled quantitative values.</p>
<h2 id="correlation">Correlation</h2>
<p>Correlation ranges between -1 and 1:</p>
<ul>
<li>1: perfect positive linear relationship.</li>
<li>0: no correlation.</li>
<li>-1: perfect negative linear relationship.</li>
</ul>
<h2 id="bootstrapping">Bootstrapping</h2>
<p>Bootstrapping is a technique for estimating the bias/variance of an estimator by repeatedly resampling with replacement. In R, use the <code>boot()</code> function in the <code>boot</code> package.</p>
<h2 id="paste"><code>paste()</code></h2>
<p>The <code>paste()</code> function concatenates multiple character vectors into a single vector.</p>
<h2 id="chi-squared-test">Chi-squared test</h2>
<p>Used for statistical tests of categorical data, such as goodness of fit and independence.</p>
<h2 id="plotting-the-regression-line">Plotting the regression line</h2>
<pre><code class="language-r">&gt; plot(x, y)
&gt; abline(lm.result)
</code></pre>
<h2 id="coefficients-of-regression">Coefficients of regression</h2>
<p>The <code>coef()</code> function returns a vector of coefficients:</p>
<pre><code class="language-r">&gt; coef(lm.result)
</code></pre>
<h2 id="anova">ANOVA</h2>
<p>Analysis of Variance (ANOVA) compares means for more than two independent samples.</p>
<h2 id="regression-analysis">Regression analysis</h2>
<p>Used for modeling the relationship between a response variable ($y$) and one or more predictors ($x$).</p>
<ul>
<li>$p=1$: simple regression.</li>
<li>$p&gt;1$: multiple regression.</li>
</ul>
<h2 id="iid">i.i.d.</h2>
<p>Independent and identically distributed.</p>
<h2 id="linear-models">Linear models</h2>
<p>Despite the name, linear models are flexible and can model curved relationships if predictors are transformed.</p>
<h2 id="failing-to-reject-the-null-hypothesis">Failing to reject the null hypothesis</h2>
<p>Failing to reject the null hypothesis doesn’t mean you “accept” it; it may just mean there is insufficient data or obscured relationships (e.g., due to outliers).</p>
<h2 id="lurking-variables">Lurking variables</h2>
<p>A lurking variable $Z$ might be the real driver behind an observed relationship between $x$ and $y$.</p>
<h2 id="statistical-inference-assumptions">Statistical inference assumptions</h2>
<p>Data must be independent and identically distributed (i.i.d.).</p>]]></content:encoded>
    </item>
  </channel>
</rss>
