• Home
  • Getting Started
    • What is R?
    • Installing R / RStudio
    • Learning the Interface
    • Learning the Verbiage
    • The Basics
    • Packages
  • Input
    • Creating Data in R
    • Data From External Sources>
      • CSV
      • Database
      • Excel
      • SAS
      • SPSS
      • Stata
      • txt
    • Sample Data from R Packages
  • Data Management
    • Viewing Data
    • Merging Data
    • Filtering Data
    • Sorting Data
    • Operators>
      • Arithmetic Operators
      • Comparative Operators
      • Logical Operators
    • Functions>
      • Numeric Functions
      • Data Conversion Functions
      • Summary (Statistical) Functions
  • Data Visualization
    • Base Graphics>
      • Bar Plot>
        • Bar Plot Basics
        • Add Reference Lines
        • Customize Axis
        • Customize Bars
        • Customize Labels
        • Customize Layout
      • Box Plot>
        • Box Plot Basics
        • Add Reference Lines
      • Histogram>
        • Histogram Basics
      • Density Plot>
        • Density Plot Basics
      • Dot Plot>
        • Dot Plot Basics
      • Line Chart>
        • Line Chart Basics
      • Pie Chart>
        • Pie Chart Basics
      • Scatter Plot>
        • Scatter Plot Basics
    • ggplot2>
      • Box Plot
      • Density Plot
      • Scatter Plot
      • Histogram
  • Statistics
    • Basic Statistics>
      • Descriptive Statistics
      • Simple Linear Regression
      • Multiple Regression
      • t - tests
      • ANOVA
      • Sample Size / Power Analysis
  • Output
    • Data Output>
      • CSV
      • Excel
      • SAS
      • SPSS
      • Stata
      • txt
    • Visualization Output>
      • JPEG
      • PDF
      • PNG
      • Windows MetaFile
 How To in R
Multiple Regression

Generating the Model
Fitting data with a multiple regression can be performed via the lm function. In order to view the results of the fit, a user must use the summary function. 

fit = lm (ResponseVariable ~ PredictorVariable1 + Predictor Variable2 + PredictorVariable3 ... ,  data = ElementName)
summary( fit )

Example:
> fit = lm (stack.loss ~ Air.Flow + Water.Temp + Acid.Conc., data = stackloss) 
> summary ( fit )
Picture

Viewing Diagnostic Plots
The common diagnostic plots can be viewed by plotting the fit result. The user can display all four diagnostic plots at once by defining a layout scheme. 

fit = lm (ResponseVariable ~ PredictorVariable1 + Predictor Variable2 + PredictorVariable3 ... ,  data = ElementName)
layout ( matrix ( c( 1,2,3,4 ), 2, 2 ))
plot ( fit )

Example:
> fit = lm (stack.loss ~ Air.Flow + Water.Temp + Acid.Conc., data = stackloss) 
> layout ( matrix( c( 1,2,3,4 ), 2, 2 ))
> plot ( fit )

Picture

Viewing Model Coefficients
Model coefficients can be viewed via the coefficients function. 

fit = lm (ResponseVariable ~ PredictorVariable1 + Predictor Variable2 + PredictorVariable3 ... ,  data = ElementName)
coefficients ( fit )


Example:
> fit = lm (stack.loss ~ Air.Flow + Water.Temp + Acid.Conc., data = stackloss) 
> coefficients ( fit )
Picture

Viewing Confidence Intervals for Model Parameters
The confidence intervals for the model parameters can be viewed via the confint function. 

fit = lm (ResponseVariable ~ PredictorVariable1 + Predictor Variable2 + PredictorVariable3 ... ,  data = ElementName)
confint (fit , level = ConfidenceLevel )


Example:
> fit = lm (stack.loss ~ Air.Flow + Water.Temp + Acid.Conc., data = stackloss) 
> confint ( fit , level = 0.95)

Picture

Viewing Predicted Fit Values
The fitted values can be viewed via the fitted function.

fit = lm (ResponseVariable ~ PredictorVariable1 + Predictor Variable2 + PredictorVariable3 ... ,  data = ElementName)
fitted ( fit )


Example:
> fit = lm (stack.loss ~ Air.Flow + Water.Temp + Acid.Conc., data = stackloss) 
> fitted ( fit )

Picture

Viewing Residuals
The residuals can be viewed via the residuals function. 

fit = lm (ResponseVariable ~ PredictorVariable1 + Predictor Variable2 + PredictorVariable3 ... ,  data = ElementName)
residuals ( fit )


Example:
> fit = lm (stack.loss ~ Air.Flow + Water.Temp + Acid.Conc., data = stackloss) 
> residuals ( fit )

Picture

Viewing ANOVA Table
The ANOVA table can be viewed via the anova function. 

fit = lm (ResponseVariable ~ PredictorVariable1 + Predictor Variable2 + PredictorVariable3 ... ,  data = ElementName)
anova ( fit )


Example:
> fit = lm (stack.loss ~ Air.Flow + Water.Temp + Acid.Conc., data = stackloss) 
> anova ( fit )

Picture

Viewing Covariance Matrix for Model Parameters 
The covariance matrix can be viewed via the vcov function. 

fit = lm (ResponseVariable ~ PredictorVariable1 + Predictor Variable2 + PredictorVariable3 ... ,  data = ElementName)
vcov ( fit )


Example:
> fit = lm (stack.loss ~ Air.Flow + Water.Temp + Acid.Conc., data = stackloss) 
> vcov ( fit )

Picture

Assessing Relative Importance of each Predictor Variable
The relative importance of each predictor variable can be assessed via the functions available in the relaimpo package. 

fit = lm (ResponseVariable ~ PredictorVariable1 + Predictor Variable2 + PredictorVariable3 ... ,  data = ElementName)
install.packages ( "relaimpo" )
library ( relaimpo )
Relimp =  boot.relimp ( fit , type = "lmg"  , rela = TRUE)
plot ( booteval.relimp ( Relimp , sort = TRUE ))


Example:
> fit = lm (stack.loss ~ Air.Flow + Water.Temp + Acid.Conc., data = stackloss) 
> install.packages ( "relaimpo" )
> library ( relaimpo )
> Relimp =  boot.relimp ( fit , type = "lmg"  , rela = TRUE)
> plot ( booteval.relimp ( Relimp , sort = TRUE ))

Picture

Powered by Create your own unique website with customizable templates.