# How to do a linear model in R?

The objective of this research was to investigate the long-term effects of irrigation with treated waste water on some chemical soil properties.

The investigation was carried out by comparison of soil properties in two different fields: one irrigated with the effluent from Parkan Waste water Treatment Plant over a period of six years, and the other one irrigated with water over the same period of time. Soil samples were taken from different depths of 0-15, 15-30, 30-60, 60-100 and 100-150 cm in both fields, and analyzed for various chemical properties.

For visual summaries, I am going to plot depths of soil and element (with line and bar plots). However, I also need to fit a linear model with one categorical and one continuous predictor. How can this be done in R?

-

The syntax is actually absurdly simple:

mymodel <- lm(dependentvariable ~ continuousvariable + categoricalvariable,
data=yourdata).


You can then call summary() to get the coefficients, and plot() to examine the residuals. HTH.

-
In this particular case, I think it would be best to further clarify the R syntax and meaning of summary and plot, or to give external links that help to understand how to interpret LM output in R. Maybe John Maindonald or Peter Dalgaard's tutorials, or directly the CRAN contrib docs? –  chl Apr 2 '11 at 10:41
First you have to enter your data into R, see this class note. You can follow the steps of this tutorial in the analysis, section 4.4 has a very similar example. In visualization you could do something similar as the qplot(wt, mpg, data = mtcars, colour = factor(cyl)) example of this tutorial.
Even though you link to the help page, I'd probably put a library(ggplot2) before qplot as that code will not run in a fresh R session w/o loading the appropriate package. –  Chase Apr 2 '11 at 14:05