Difference between revisions of "R Regression"
Line 15: | Line 15: | ||
regres <- lm(lwage~exper+log(huswage),data=mydata) | regres <- lm(lwage~exper+log(huswage),data=mydata) | ||
− | So let's look at the last line in which we ask R to run a regression. Whatever comes in the parenthesis after <source enclose=none>lm</source> are parameters to the <source enclose=none>lm()</source> function. Different parameters are separated by commas. So here we have two inputs. Let's start with the second <source enclose=none>data=mydata</source>. This basically indicates to R that we are drawing the data for the regression from our dataframe called <source enclose=none>mydata</source>. That means that for the first input, in which we actually specify the model we estimate, we can refer to the variable names of the variables that are contained in <source enclose=none>mydata</source>. In that first input you should imagine writing down a regression model | + | So let's look at the last line in which we ask R to run a regression. Whatever comes in the parenthesis after <source enclose=none>lm</source> are parameters to the <source enclose=none>lm()</source> function. Different parameters are separated by commas. So here we have two inputs. Let's start with the second <source enclose=none>data=mydata</source>. This basically indicates to R that we are drawing the data for the regression from our dataframe called <source enclose=none>mydata</source>. That means that for the first input, in which we actually specify the model we estimate, we can refer to the variable names of the variables that are contained in <source enclose=none>mydata</source>. In that first input you should imagine writing down a regression model. The model we want to estimate is the following: |
+ | <math>\label{OLSModel} | ||
+ | y = \beta_0 + \beta_1 * exper + \beta_2 * log(huswage) + \epsilon</math> | ||
− | < | + | The way how you tell <source enclose=none>lm()</source> to |
− |
Revision as of 16:40, 17 January 2015
Let's assume we want to run a regression with lwage
(the logarithm of the woman's wage) as dependent variable and a constant, exper
(the years of experience) and the logarithm of the husbands wage (huswage
as explanatory variables. First we should note that the logarithm of the woman's wage already exists as variable lwage
, but the logarithm of the husband's wage doesn't exist as its own variable. Hence we are yet to calculate it.
The lm()
function
The R function that does the heavy lifting for regression analysis is the lm()
function (presumably an abbreviation for "linear model")and we will have a close up look at how it works. But let's get our first regression under the belt. The following few lines of code (which you should save in a script) import the data, convert missing data to NAs (see R_Data#Data_Types) and eventually runs a regression:
# This is my first R regression! setwd("T:/ECLR/R/FirstSteps") # This sets the working directory mydata <- read.csv("mroz.csv") # Opens mroz.csv from working directory # Now convert variables with "." to num with NA mydata$
wage <- as.numeric(as.character(mydata$
wage)) mydata$
lwage <- as.numeric(as.character(mydata$
lwage)) # Run a regression regres <- lm(lwage~exper+log(huswage),data=mydata)
So let's look at the last line in which we ask R to run a regression. Whatever comes in the parenthesis after lm
are parameters to the lm()
function. Different parameters are separated by commas. So here we have two inputs. Let's start with the second data=mydata
. This basically indicates to R that we are drawing the data for the regression from our dataframe called mydata
. That means that for the first input, in which we actually specify the model we estimate, we can refer to the variable names of the variables that are contained in mydata
. In that first input you should imagine writing down a regression model. The model we want to estimate is the following:
[math]\label{OLSModel} y = \beta_0 + \beta_1 * exper + \beta_2 * log(huswage) + \epsilon[/math]
The way how you tell lm()
to