R Tutorial: R objects for statistical modeling

Learn the three core R objects for statistical modeling: data frames (collections of named variables), formulas (using ~ to specify relationships between variables), and functions (which construct models and compute outputs). An example with the CPS85 dataset shows how formulas let you calculate average wages by sector.
Chapters
In the first video, I said that a mathematical model is a model built for mathematical stuff. A statistical model is a mathematical model that's closely tied to data. In practice, statistical models are built from a special kind of mathematical stuff, the stuff that makes up computer languages. In this video, we'll examine some of the kinds of objects in R that you will encounter in your work with models. Three of the most important R objects for modeling are functions, formulas, and data frames. Some people describe a data frame as a kind of spreadsheet or matrix with rows and columns. I prefer to think of data frames more simply as a collection of variables. Each of the variables is a column. And in modeling, it's a good practice to give a name to each of the variables in the in the data frame. That makes it easier to describe the models in terms of the names of the variables. The rows of a data frame are called cases. Each case is one object. In the real world, case might be a person, or it might be a person at a particular time or anything else. But always the case is the object from which values for the individual variables are measured. We're going to use functions for several purposes, both to build models and to evaluate those models. For instance, to calculate the output of models for new inputs. The functions that build models will generally take as their inputs both a data frame and a formula that describes the relationship among the variables involved in the model. Formulas are a way to describe how you want to relate variables to one another. In a formula, variable names are used, but no calculation is done with the values in those variables. Instead, the formula sets up the structure of the relationship that the modeler wants to express or explore. All formulas involve the little squiggle symbol called tilde. There will always be something to the right of the tilde, typically one or more variables separated by punctuation that looks like arithmetic but isn't. To start, that punctuation will be the plus sign. But later you will see other forms of punctuation. As an example of how functions, formulas and data frames are used together, let's use Data set called CPS85 and calculate the mean wage of workers in several different sectors of the economy. The mean function is one of the first functions newcomers to R encounter, but it isn't set up to use formulas. The Mosaic package upgrades mean and other functions so that they work with formulas while continuing to work in the original way. The formula wage till the sector means to break down the wage by sector. Using that formula in the mean function gives the average wage in each sector. Statistical models are often built to predict or account for a single variable, which we will call the response variable. The basic idea is to construct a function that produces values for the response variable as the function's output. The function's inputs are called the explanatory variables. In formulas for models, the response variable is always to the left of the tilde. The explanatory variables are to the right of the tilde. You can think of formulas as a sentence that relates the response and the explanatory variables. There are several English equivalents to tilde. For example, wage. Tilde sector can be read as any of wage as a function of sector wage accounted for by sector wage modeled by sector wage explained by sector wage given sector wage broken down by sector.
Sorry to be late. And you are Dr. Null of the null hypothesis. Meaning the null hypothesis is that nothing is happening, that variation is nothing but randomness.
Okay, but this is a course on statistical modeling. Our object is to account for and explain variation. Sure, there's some randomness, but it's what's left over after our accounting for the rest.
But every statistics course needs me.
I think you're looking for the T test course. This is statistical modeling. Why? Let's get back to building models.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







