Difference between revisions of "R Regression"

From ECLR
Jump to: navigation, search
(The lm() function)
Line 4: Line 4:
 
== The <source enclose=none>lm()</source> function ==
 
== The <source enclose=none>lm()</source> function ==
  
The R function that does the heavy lifting for regression analysis is the <source enclose=none>lm()</source> function 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:
+
The R function that does the heavy lifting for regression analysis is the <source enclose=none>lm()</source> function 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!
 
     # This is my first R regression!

Revision as of 16:31, 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 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)