Spatial Ecology & R
  • Research & Training
  • R Code & Data
  • Other Resources

Spatial Ecology & R
(search via "category" below)

 

10/26/2016

0 Comments

 

R Tip: Linear Regression

Anonymous

October 05, 2016

Linear Regression is a common statistical tool used to determine relationships between data This RTip is modified from: http://www.apsnet.org/edcenter/advanced/topics/EcologyAndEpidemiologyInR/DiseaseProgress/Pages/LinearRegression.aspx

install.packages("stats")
## Error in install.packages : Updating loaded packages
library(stats)

For example, let’s look at the relationship between girth and height of black cherry trees First, look at the data

head(trees)
##   Girth Height Volume
## 1   8.3     70   10.3
## 2   8.6     65   10.3
## 3   8.8     63   10.2
## 4  10.5     72   16.4
## 5  10.7     81   18.8
## 6  10.8     83   19.7
plot(trees$Girth, trees$Height, ylab = "Height", xlab = "Girth", pch=21, bg="royalblue")

Next, run the linear model

trees.lm<-lm(trees$Height~trees$Girth, data=trees)

And view the stats

summary.lm(trees.lm)
## 
## Call:
## lm(formula = trees$Height ~ trees$Girth, data = trees)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -12.5816  -2.7686   0.3163   2.4728   9.9456 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  62.0313     4.3833  14.152 1.49e-14 ***
## trees$Girth   1.0544     0.3222   3.272  0.00276 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 5.538 on 29 degrees of freedom
## Multiple R-squared:  0.2697, Adjusted R-squared:  0.2445 
## F-statistic: 10.71 on 1 and 29 DF,  p-value: 0.002758

The linear fit line has an intercept of 62.0313, an slope of 1.0544, and an R2 value of 0.2697

#Finally, add the linear fit line
plot(trees$Girth, trees$Height, ylab = "Height", xlab = "Girth", pch=21, bg="royalblue")
abline(trees.lm, col="black", lty=1)

save.image("Lab4RTip.RData")
0 Comments



Leave a Reply.

    Spatial Ecology @ MSU

    Click on "Category" below to search for R code compiled by the Zarnetske Spatial & Community Ecology Lab and students in MSU's Spatial Ecology graduate course (FOR870/FW870)

    Category

    All
    Apply
    Cite R Package
    Clip Shapefile
    Data: Climate
    Data: Fire
    Data Manipulation
    Data: Spatial
    Data: Species
    Data: Vegetation
    Dates
    Debugging
    Distributions
    Gbif
    Glm
    Leaflet (R Package)
    Mapping
    Mapzen
    Plotting
    Polygon
    Projections
    Raster Stack
    Reclassify Raster
    R Markdown
    R Package: Dismo
    R Package: Ggplot2
    R Package: Maps
    R Package: Raster
    R Packages
    R Package: Tree
    R Package: Unmarked
    R Vignettes
    SDM: Bioclim
    SDM: Classification & Regression Trees
    SDM: Domain
    SDM: Gam
    SDM: Glm
    SDM: HSDM
    SDM: Maxent
    SDM: Random Forests
    Shapefile
    Spatial Clustering
    Tapply
    Text
    Time
    Variogram

    Archive

    October 2016
    September 2016
    December 2015
    November 2015
    October 2015
    September 2015

    RSS Feed

Powered by Create your own unique website with customizable templates.
  • Research & Training
  • R Code & Data
  • Other Resources