SlideShare a Scribd company logo
1 of 31
Download to read offline
Simple linear regression uses a independent variable
to predict the outcome of a dependent variable. This
is the most basic form of regression, numerous
complex modeling techniques can be learned by
understanding this basic complex.
In R several classical statistical models can be
implemented using the function: lm (linear model).
The lm function can be used for simple and multiple
linear regression
> ?lm
In R several classical statistical models can be
implemented using the function: lm (linear model).
The lm function can be used for simple and multiple
linear regression
> ?lm
Usage
lm(formula, data, subset, weights, na.action,
method = "qr", model = TRUE, x = FALSE, y = FALSE,
qr = TRUE, singular.ok = TRUE, contrasts = NULL,
offset, ...)
The first argument in the lm function (formula) is
where you specify the structure of the statistical
model.
Common structure of a statistical model is:
y ~x Simple linear regression of y on x
y ~ x + z Multiple regression of y on x and z
>
https://drive.google.com/file/d/0B4-nNA2Ua3DoUmJMQ3JJTFpzTDg/view?usp=sharing
To demonstrate simple linear regression in R, we will
again use the Macedonian Soil Dataset. Here we will
regress Soil Organic Carbon on DEM.
DSM_table <- read.csv("DSM_table2.csv")
To demonstrate simple linear regression in R, we will
again use the Macedonian Soil Dataset. Here we will
regress Soil Organic Carbon on DEM.
> head(DSM_table2)
ID ProfID X Y UpperDepth LowerDepth Value Lambda
1 4 P0004 7485085 4653725 0 30 11.878804 0.1
2 7 P0007 7486492 4653203 0 30 3.490205 0.1
3 8 P0008 7485564 4656242 0 30 2.317673 0.1
4 9 P0009 7495075 4652933 0 30 1.936148 0.1
5 10 P0010 7494798 4651945 0 30 1.339719 0.1
6 11 P0011 7492500 4651760 0 30 2.285384 0.1
tsme slp prec dem
1 0.160096433 13 998.034 2327
2 0.002569598 35 1014.300 1986
3 0.002601836 6 779.994 1243
4 0.002841078 25 839.183 1120
5 0.002677120 30 843.919 1098
The summary statistics,
> summary(cbind(SOC = DSM_table$Value, Slope =DSM_table2$slp,
Precipitation=DSM_table2$prec, DEM=DSM_table$dem))
SOC Slope Precipitation DEM
Min. : 0.000 Min. : 0.000 Min. : 424.5 Min. : 45.0
1st Qu.: 1.005 1st Qu.: 0.000 1st Qu.: 532.3 1st Qu.: 404.2
Median : 1.493 Median : 3.000 Median : 564.3 Median : 592.0
Mean : 1.912 Mean : 7.414 Mean : 597.5 Mean : 642.3
3rd Qu.: 2.244 3rd Qu.:11.000 3rd Qu.: 641.8 3rd Qu.: 768.0
Max. :50.332 Max. :56.000 Max. :1180.3 Max. :2375.0
NA's :1
Our hypothesis here is that elevation is a good
predictor of SOC!?.To start, let’s have a look at what
the data looks like
plot(DSM_table$Value, DSM_table$dem)
Our hypothesis here is that elevation is a good
predictor of SOC!?.To start, let’s have a look at what
the data looks like
plot(DSM_table$Value, DSM_table$dem)
There appears there is not meaningful. To fit a linear
model, we can use the lm function:
model1 <- lm(Value ~ dem, data=DSM_table, y=TRUE, x = TRUE)
> model1
Call:
lm(formula = Value ~ dem, data = DSM_table, x = TRUE, y = TRUE)
Coefficients:
(Intercept) dem1
0.715117 0.001856
> summary(model1)
Call:
lm(formula = Value ~ dem1, data = DSM_table, x = TRUE, y = TRUE)
Residuals:
Min 1Q Median 3Q Max
-3.917 -0.769 -0.224 0.389 48.895
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 7.151e-01 6.929e-02 10.32 <2e-16 ***
dem1 1.856e-03 9.367e-05 19.82 <2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 1.954 on 3300 degrees of freedom
Multiple R-squared: 0.1063, Adjusted R-squared: 0.1061
F-statistic: 392.6 on 1 and 3300 DF, p-value: < 2.2e-16
> class(model1)
[1] "lm"
the output from the lm function is an object of class
lm. An object of class "lm" is a list containing at least
the following components:
> class(model1)
[1] "lm"
the output from the lm function is an object of class
lm. An object of class "lm" is a list containing at least
the following components:
coefficients - a named vector of coefficients
residuals - the residuals, that is response minus fitted values.
fitted.values - the fitted mean values.
rank - the numeric rank of the fitted linear model.
weights - (only for weighted fits) the specified weights.
df.residual -the residual degrees of freedom.
call - the matched call.
terms - the terms object used.
contrasts - (only where relevant) the contrasts used.
xlevels -(only where relevant) a record of the levels of the factors
used in fitting.
offset- the offset used (missing if none were used).
y - if requested, the response used.
x- if requested, the model matrix used.
model - if requested (the default), the model frame used.
na.action - (where relevant) information returned by model.frame on
the special handling of NAs.
class(model1)
[1] "lm"
model1$coefficients
(Intercept) dem1
0.715116901 0.001856089
> formula(model1)
Value ~ dem1
the output from the lm function is an object of class
lm. An object of class "lm" is a list containing at least
the following components:
head(residuals(model1))
1 2 3 4 5 6
6.8445691 -1.2674728 -0.7045624 -0.5960802 -1.1628119 -1.1990168
names(summary(model1))
[1] "call" "terms" "residuals" "coefficients"
[5] "aliased" "sigma" "df" "r.squared"
[9] "adj.r.squared" "fstatistic" "cov.unscaled"
Here is a list of what is available from the summary
function for this model:
summary(model1)[[4]]
Estimate Std. Error t value Pr(>|t|)
(Intercept) 0.715116901 6.928677e-02 10.32112 1.333183e-24
dem1 0.001856089 9.367314e-05 19.81452 1.188533e-82
summary(model1)[[7]]
[1] 2 3300 2
To extract some of the information from the
summary which is of a list structure, we can use:
> summary(model1)[["r.squared"]]
[1] 0.1063245
> summary(model1)[[8]]
[1] 0.1063245
What is the RSquared of model1?
> summary(model1)[["r.squared"]]
[1] 0.1063245
> summary(model1)[[8]]
[1] 0.1063245
What is the RSquared of model1?
head(predict(model1))
1 2 3 4 5 6
5.034235 4.757678 3.022235 2.532228 2.502530 3.484401
head(DSM_table$Value)
[1] 11.878804 3.490205 2.317673 1.936148 1.339719 2.285384
head(residuals(model1))
1 2 3 4 5 6
6.8445691 -1.2674728 -0.7045624 -0.5960802 -1.1628119 -1.1990168
head(model2$residuals)
1 2 3 4 5 6
7.3395541 -1.1690560 -0.5363049 -1.2854938 -1.9692882 -1.5173124
> head(model2$fitted.values)
1 2 3 4 5 6
4.539250 4.659261 2.853978 3.221641 3.309007 3.802697
plot(model1$y, model1$fitted.values)
Lets plot() the observed vs. predicted from
the model
model2subset <-DSM_table2[, c("Value", "slp", "prec", "dem")]
summary(model2subset)
Value slp prec dem
Min. : 0.000 Min. : 0.000 Min. : 424.5 Min. : 45.0
1st Qu.: 1.005 1st Qu.: 0.000 1st Qu.: 532.3 1st Qu.: 404.2
Median : 1.493 Median : 3.000 Median : 564.3 Median : 592.0
Mean : 1.912 Mean : 7.414 Mean : 597.5 Mean : 642.3
3rd Qu.: 2.244 3rd Qu.:11.000 3rd Qu.: 641.8 3rd Qu.: 768.0
Max. :50.332 Max. :56.000 Max. :1180.3 Max. :2375.0
NA's :1
We will regress SOC on Precipitation,
Slope and Elevation. First lets subset these
data out, then get their summary statistics
cor(na.omit(model2subset))
Value slp prec dem
Value 1.0000000 0.2730310 0.3155474 0.3317814
slp 0.2730310 1.0000000 0.5765489 0.6011170
prec 0.3155474 0.5765489 1.0000000 0.8158338
dem 0.3317814 0.6011170 0.8158338 1.0000000
A quick way to look for relationships between
variables in a data frame is with the cor function.
Note the use of the na.omit function.
> pairs(na.omit(model2subset))
To visualize these relationships, we can use pairs
> pairs(na.omit(model2subset))
To visualize these relationships, we can use pairs
model2 <- lm(Value ~ slp + prec + dem, data = model2subset)
model2
Call:
lm(formula = Value ~ slp + prec + dem, data = model2subset)
Coefficients:
(Intercept) slp prec dem
-0.027413 0.020314 0.001868 0.001048
fitting the multiple linear regression,
summary(model2)
Call:
lm(formula = Value ~ slp + prec + dem, data = model2subset)
Residuals:
Min 1Q Median 3Q Max
-3.527 -0.717 -0.219 0.379 48.907
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -0.0274134 0.2240410 -0.122 0.902622
slp 0.0203135 0.0041948 4.843 1.34e-06 ***
prec 0.0018682 0.0004956 3.770 0.000166 ***
dem 0.0010477 0.0001681 6.232 5.18e-10 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 1.937 on 3297 degrees of freedom
(1 observation deleted due to missingness)
Multiple R-squared: 0.1223, Adjusted R-squared: 0.1215
F-statistic: 153.2 on 3 and 3297 DF, p-value: < 2.2e-16
TASK1:
Regress SOC (Value) on; Slope (slp), Elevation (dem), TWI
(twi), Annual Nightly Mean Temperature (tmpn), Annual Daily
Mean Temperature (tmpd)
TASK2: Share you results here,
https://goo.gl/zlNcb5
Sample Data Sheet
https://goo.gl/g5NQCv
TASK1:
Regress SOC (Value) on; Slope (slp), Elevation (dem), TWI
(twi), Annual Nightly Mean Temperature (tmpn), Annual Daily
Mean Temperature (tmpd)
TASK2: Share your results here,
https://goo.gl/zlNcb5
Sample Data
https://goo.gl/g5NQCv

More Related Content

What's hot

Time Series Analysis and Mining with R
Time Series Analysis and Mining with RTime Series Analysis and Mining with R
Time Series Analysis and Mining with RYanchang Zhao
 
Multiclassification with Decision Tree in Spark MLlib 1.3
Multiclassification with Decision Tree in Spark MLlib 1.3Multiclassification with Decision Tree in Spark MLlib 1.3
Multiclassification with Decision Tree in Spark MLlib 1.3leorick lin
 
R programming intro with examples
R programming intro with examplesR programming intro with examples
R programming intro with examplesDennis
 
Data manipulation on r
Data manipulation on rData manipulation on r
Data manipulation on rAbhik Seal
 
Basic R Data Manipulation
Basic R Data ManipulationBasic R Data Manipulation
Basic R Data ManipulationChu An
 
Time series-mining-slides
Time series-mining-slidesTime series-mining-slides
Time series-mining-slidesYanchang Zhao
 
Rewriting Engine for Process Algebras
Rewriting Engine for Process AlgebrasRewriting Engine for Process Algebras
Rewriting Engine for Process AlgebrasAnatolii Kmetiuk
 
[1062BPY12001] Data analysis with R / week 2
[1062BPY12001] Data analysis with R / week 2[1062BPY12001] Data analysis with R / week 2
[1062BPY12001] Data analysis with R / week 2Kevin Chun-Hsien Hsu
 
Row Pattern Matching in SQL:2016
Row Pattern Matching in SQL:2016Row Pattern Matching in SQL:2016
Row Pattern Matching in SQL:2016Markus Winand
 
L5 determination of natural frequency & mode shape
L5 determination of natural frequency & mode shapeL5 determination of natural frequency & mode shape
L5 determination of natural frequency & mode shapeSam Alalimi
 
Comparative study of algorithms of nonlinear optimization
Comparative study of algorithms of nonlinear optimizationComparative study of algorithms of nonlinear optimization
Comparative study of algorithms of nonlinear optimizationPranamesh Chakraborty
 
Time Series Analysis:Basic Stochastic Signal Recovery
Time Series Analysis:Basic Stochastic Signal RecoveryTime Series Analysis:Basic Stochastic Signal Recovery
Time Series Analysis:Basic Stochastic Signal RecoveryDaniel Cuneo
 

What's hot (20)

Time Series Analysis and Mining with R
Time Series Analysis and Mining with RTime Series Analysis and Mining with R
Time Series Analysis and Mining with R
 
Rsplit apply combine
Rsplit apply combineRsplit apply combine
Rsplit apply combine
 
Multiclassification with Decision Tree in Spark MLlib 1.3
Multiclassification with Decision Tree in Spark MLlib 1.3Multiclassification with Decision Tree in Spark MLlib 1.3
Multiclassification with Decision Tree in Spark MLlib 1.3
 
R programming intro with examples
R programming intro with examplesR programming intro with examples
R programming intro with examples
 
Data manipulation on r
Data manipulation on rData manipulation on r
Data manipulation on r
 
Basic R Data Manipulation
Basic R Data ManipulationBasic R Data Manipulation
Basic R Data Manipulation
 
Time series-mining-slides
Time series-mining-slidesTime series-mining-slides
Time series-mining-slides
 
Rewriting Engine for Process Algebras
Rewriting Engine for Process AlgebrasRewriting Engine for Process Algebras
Rewriting Engine for Process Algebras
 
E33018021
E33018021E33018021
E33018021
 
CLUSTERGRAM
CLUSTERGRAMCLUSTERGRAM
CLUSTERGRAM
 
[1062BPY12001] Data analysis with R / week 2
[1062BPY12001] Data analysis with R / week 2[1062BPY12001] Data analysis with R / week 2
[1062BPY12001] Data analysis with R / week 2
 
Numerical integration
Numerical integrationNumerical integration
Numerical integration
 
Row Pattern Matching in SQL:2016
Row Pattern Matching in SQL:2016Row Pattern Matching in SQL:2016
Row Pattern Matching in SQL:2016
 
MATLAB ARRAYS
MATLAB ARRAYSMATLAB ARRAYS
MATLAB ARRAYS
 
L5 determination of natural frequency & mode shape
L5 determination of natural frequency & mode shapeL5 determination of natural frequency & mode shape
L5 determination of natural frequency & mode shape
 
130717666736980000
130717666736980000130717666736980000
130717666736980000
 
Comparative study of algorithms of nonlinear optimization
Comparative study of algorithms of nonlinear optimizationComparative study of algorithms of nonlinear optimization
Comparative study of algorithms of nonlinear optimization
 
Time Series Analysis:Basic Stochastic Signal Recovery
Time Series Analysis:Basic Stochastic Signal RecoveryTime Series Analysis:Basic Stochastic Signal Recovery
Time Series Analysis:Basic Stochastic Signal Recovery
 
R learning by examples
R learning by examplesR learning by examples
R learning by examples
 
R code for data manipulation
R code for data manipulationR code for data manipulation
R code for data manipulation
 

Similar to 11. Linear Models

Linear models
Linear modelsLinear models
Linear modelsFAO
 
curve fitting or regression analysis-1.pptx
curve fitting or regression analysis-1.pptxcurve fitting or regression analysis-1.pptx
curve fitting or regression analysis-1.pptxabelmeketa
 
MULTICOLLINERITY.pptx
MULTICOLLINERITY.pptxMULTICOLLINERITY.pptx
MULTICOLLINERITY.pptxYanYingLoh
 
Econometric Analysis 8th Edition Greene Solutions Manual
Econometric Analysis 8th Edition Greene Solutions ManualEconometric Analysis 8th Edition Greene Solutions Manual
Econometric Analysis 8th Edition Greene Solutions ManualLewisSimmonss
 
BALLANDBEAM_GROUP7.pptx
BALLANDBEAM_GROUP7.pptxBALLANDBEAM_GROUP7.pptx
BALLANDBEAM_GROUP7.pptxOthmanBensaoud
 
QR Factorizations and SVDs for Tall-and-skinny Matrices in MapReduce Architec...
QR Factorizations and SVDs for Tall-and-skinny Matrices in MapReduce Architec...QR Factorizations and SVDs for Tall-and-skinny Matrices in MapReduce Architec...
QR Factorizations and SVDs for Tall-and-skinny Matrices in MapReduce Architec...Austin Benson
 
Optimum Algorithm for Computing the Standardized Moments Using MATLAB 7.10(R2...
Optimum Algorithm for Computing the Standardized Moments Using MATLAB 7.10(R2...Optimum Algorithm for Computing the Standardized Moments Using MATLAB 7.10(R2...
Optimum Algorithm for Computing the Standardized Moments Using MATLAB 7.10(R2...Waqas Tariq
 
Comparisons
ComparisonsComparisons
ComparisonsZifirio
 
Comparisons
ComparisonsComparisons
ComparisonsZifirio
 
Comparisons
ComparisonsComparisons
ComparisonsZifirio
 
Comparisons
ComparisonsComparisons
ComparisonsZifirio
 
Comparisons
ComparisonsComparisons
ComparisonsZifirio
 
Comparisons
ComparisonsComparisons
ComparisonsZifirio
 
Comparisons
ComparisonsComparisons
ComparisonsZifirio
 
Comparisons
ComparisonsComparisons
ComparisonsZifirio
 
Comparisons
ComparisonsComparisons
ComparisonsZifirio
 
Comparisons
ComparisonsComparisons
ComparisonsZifirio
 

Similar to 11. Linear Models (20)

Linear models
Linear modelsLinear models
Linear models
 
Learn Matlab
Learn MatlabLearn Matlab
Learn Matlab
 
R Programming Homework Help
R Programming Homework HelpR Programming Homework Help
R Programming Homework Help
 
curve fitting or regression analysis-1.pptx
curve fitting or regression analysis-1.pptxcurve fitting or regression analysis-1.pptx
curve fitting or regression analysis-1.pptx
 
MULTICOLLINERITY.pptx
MULTICOLLINERITY.pptxMULTICOLLINERITY.pptx
MULTICOLLINERITY.pptx
 
Econometric Analysis 8th Edition Greene Solutions Manual
Econometric Analysis 8th Edition Greene Solutions ManualEconometric Analysis 8th Edition Greene Solutions Manual
Econometric Analysis 8th Edition Greene Solutions Manual
 
BALLANDBEAM_GROUP7.pptx
BALLANDBEAM_GROUP7.pptxBALLANDBEAM_GROUP7.pptx
BALLANDBEAM_GROUP7.pptx
 
QR Factorizations and SVDs for Tall-and-skinny Matrices in MapReduce Architec...
QR Factorizations and SVDs for Tall-and-skinny Matrices in MapReduce Architec...QR Factorizations and SVDs for Tall-and-skinny Matrices in MapReduce Architec...
QR Factorizations and SVDs for Tall-and-skinny Matrices in MapReduce Architec...
 
Optimum Algorithm for Computing the Standardized Moments Using MATLAB 7.10(R2...
Optimum Algorithm for Computing the Standardized Moments Using MATLAB 7.10(R2...Optimum Algorithm for Computing the Standardized Moments Using MATLAB 7.10(R2...
Optimum Algorithm for Computing the Standardized Moments Using MATLAB 7.10(R2...
 
R Language Introduction
R Language IntroductionR Language Introduction
R Language Introduction
 
Comparisons
ComparisonsComparisons
Comparisons
 
Comparisons
ComparisonsComparisons
Comparisons
 
Comparisons
ComparisonsComparisons
Comparisons
 
Comparisons
ComparisonsComparisons
Comparisons
 
Comparisons
ComparisonsComparisons
Comparisons
 
Comparisons
ComparisonsComparisons
Comparisons
 
Comparisons
ComparisonsComparisons
Comparisons
 
Comparisons
ComparisonsComparisons
Comparisons
 
Comparisons
ComparisonsComparisons
Comparisons
 
Comparisons
ComparisonsComparisons
Comparisons
 

More from FAO

Nigeria
NigeriaNigeria
NigeriaFAO
 
Niger
NigerNiger
NigerFAO
 
Namibia
NamibiaNamibia
NamibiaFAO
 
Mozambique
MozambiqueMozambique
MozambiqueFAO
 
Zimbabwe takesure
Zimbabwe takesureZimbabwe takesure
Zimbabwe takesureFAO
 
Zimbabwe
ZimbabweZimbabwe
ZimbabweFAO
 
Zambia
ZambiaZambia
ZambiaFAO
 
Togo
TogoTogo
TogoFAO
 
Tanzania
TanzaniaTanzania
TanzaniaFAO
 
Spal presentation
Spal presentationSpal presentation
Spal presentationFAO
 
Rwanda
RwandaRwanda
RwandaFAO
 
Nigeria uponi
Nigeria uponiNigeria uponi
Nigeria uponiFAO
 
The multi-faced role of soil in the NENA regions (part 2)
The multi-faced role of soil in the NENA regions (part 2)The multi-faced role of soil in the NENA regions (part 2)
The multi-faced role of soil in the NENA regions (part 2)FAO
 
The multi-faced role of soil in the NENA regions (part 1)
The multi-faced role of soil in the NENA regions (part 1)The multi-faced role of soil in the NENA regions (part 1)
The multi-faced role of soil in the NENA regions (part 1)FAO
 
Agenda of the launch of the soil policy brief at the Land&Water Days
Agenda of the launch of the soil policy brief at the Land&Water DaysAgenda of the launch of the soil policy brief at the Land&Water Days
Agenda of the launch of the soil policy brief at the Land&Water DaysFAO
 
Agenda of the 5th NENA Soil Partnership meeting
Agenda of the 5th NENA Soil Partnership meetingAgenda of the 5th NENA Soil Partnership meeting
Agenda of the 5th NENA Soil Partnership meetingFAO
 
The Voluntary Guidelines for Sustainable Soil Management
The Voluntary Guidelines for Sustainable Soil ManagementThe Voluntary Guidelines for Sustainable Soil Management
The Voluntary Guidelines for Sustainable Soil ManagementFAO
 
GLOSOLAN - Mission, status and way forward
GLOSOLAN - Mission, status and way forwardGLOSOLAN - Mission, status and way forward
GLOSOLAN - Mission, status and way forwardFAO
 
Towards a Global Soil Information System (GLOSIS)
Towards a Global Soil Information System (GLOSIS)Towards a Global Soil Information System (GLOSIS)
Towards a Global Soil Information System (GLOSIS)FAO
 
GSP developments of regional interest in 2019
GSP developments of regional interest in 2019GSP developments of regional interest in 2019
GSP developments of regional interest in 2019FAO
 

More from FAO (20)

Nigeria
NigeriaNigeria
Nigeria
 
Niger
NigerNiger
Niger
 
Namibia
NamibiaNamibia
Namibia
 
Mozambique
MozambiqueMozambique
Mozambique
 
Zimbabwe takesure
Zimbabwe takesureZimbabwe takesure
Zimbabwe takesure
 
Zimbabwe
ZimbabweZimbabwe
Zimbabwe
 
Zambia
ZambiaZambia
Zambia
 
Togo
TogoTogo
Togo
 
Tanzania
TanzaniaTanzania
Tanzania
 
Spal presentation
Spal presentationSpal presentation
Spal presentation
 
Rwanda
RwandaRwanda
Rwanda
 
Nigeria uponi
Nigeria uponiNigeria uponi
Nigeria uponi
 
The multi-faced role of soil in the NENA regions (part 2)
The multi-faced role of soil in the NENA regions (part 2)The multi-faced role of soil in the NENA regions (part 2)
The multi-faced role of soil in the NENA regions (part 2)
 
The multi-faced role of soil in the NENA regions (part 1)
The multi-faced role of soil in the NENA regions (part 1)The multi-faced role of soil in the NENA regions (part 1)
The multi-faced role of soil in the NENA regions (part 1)
 
Agenda of the launch of the soil policy brief at the Land&Water Days
Agenda of the launch of the soil policy brief at the Land&Water DaysAgenda of the launch of the soil policy brief at the Land&Water Days
Agenda of the launch of the soil policy brief at the Land&Water Days
 
Agenda of the 5th NENA Soil Partnership meeting
Agenda of the 5th NENA Soil Partnership meetingAgenda of the 5th NENA Soil Partnership meeting
Agenda of the 5th NENA Soil Partnership meeting
 
The Voluntary Guidelines for Sustainable Soil Management
The Voluntary Guidelines for Sustainable Soil ManagementThe Voluntary Guidelines for Sustainable Soil Management
The Voluntary Guidelines for Sustainable Soil Management
 
GLOSOLAN - Mission, status and way forward
GLOSOLAN - Mission, status and way forwardGLOSOLAN - Mission, status and way forward
GLOSOLAN - Mission, status and way forward
 
Towards a Global Soil Information System (GLOSIS)
Towards a Global Soil Information System (GLOSIS)Towards a Global Soil Information System (GLOSIS)
Towards a Global Soil Information System (GLOSIS)
 
GSP developments of regional interest in 2019
GSP developments of regional interest in 2019GSP developments of regional interest in 2019
GSP developments of regional interest in 2019
 

Recently uploaded

EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 

Recently uploaded (20)

EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 

11. Linear Models

  • 1.
  • 2.
  • 3.
  • 4. Simple linear regression uses a independent variable to predict the outcome of a dependent variable. This is the most basic form of regression, numerous complex modeling techniques can be learned by understanding this basic complex.
  • 5. In R several classical statistical models can be implemented using the function: lm (linear model). The lm function can be used for simple and multiple linear regression > ?lm
  • 6. In R several classical statistical models can be implemented using the function: lm (linear model). The lm function can be used for simple and multiple linear regression > ?lm Usage lm(formula, data, subset, weights, na.action, method = "qr", model = TRUE, x = FALSE, y = FALSE, qr = TRUE, singular.ok = TRUE, contrasts = NULL, offset, ...)
  • 7. The first argument in the lm function (formula) is where you specify the structure of the statistical model. Common structure of a statistical model is: y ~x Simple linear regression of y on x y ~ x + z Multiple regression of y on x and z > https://drive.google.com/file/d/0B4-nNA2Ua3DoUmJMQ3JJTFpzTDg/view?usp=sharing
  • 8. To demonstrate simple linear regression in R, we will again use the Macedonian Soil Dataset. Here we will regress Soil Organic Carbon on DEM. DSM_table <- read.csv("DSM_table2.csv")
  • 9. To demonstrate simple linear regression in R, we will again use the Macedonian Soil Dataset. Here we will regress Soil Organic Carbon on DEM. > head(DSM_table2) ID ProfID X Y UpperDepth LowerDepth Value Lambda 1 4 P0004 7485085 4653725 0 30 11.878804 0.1 2 7 P0007 7486492 4653203 0 30 3.490205 0.1 3 8 P0008 7485564 4656242 0 30 2.317673 0.1 4 9 P0009 7495075 4652933 0 30 1.936148 0.1 5 10 P0010 7494798 4651945 0 30 1.339719 0.1 6 11 P0011 7492500 4651760 0 30 2.285384 0.1 tsme slp prec dem 1 0.160096433 13 998.034 2327 2 0.002569598 35 1014.300 1986 3 0.002601836 6 779.994 1243 4 0.002841078 25 839.183 1120 5 0.002677120 30 843.919 1098
  • 10. The summary statistics, > summary(cbind(SOC = DSM_table$Value, Slope =DSM_table2$slp, Precipitation=DSM_table2$prec, DEM=DSM_table$dem)) SOC Slope Precipitation DEM Min. : 0.000 Min. : 0.000 Min. : 424.5 Min. : 45.0 1st Qu.: 1.005 1st Qu.: 0.000 1st Qu.: 532.3 1st Qu.: 404.2 Median : 1.493 Median : 3.000 Median : 564.3 Median : 592.0 Mean : 1.912 Mean : 7.414 Mean : 597.5 Mean : 642.3 3rd Qu.: 2.244 3rd Qu.:11.000 3rd Qu.: 641.8 3rd Qu.: 768.0 Max. :50.332 Max. :56.000 Max. :1180.3 Max. :2375.0 NA's :1
  • 11. Our hypothesis here is that elevation is a good predictor of SOC!?.To start, let’s have a look at what the data looks like plot(DSM_table$Value, DSM_table$dem)
  • 12. Our hypothesis here is that elevation is a good predictor of SOC!?.To start, let’s have a look at what the data looks like plot(DSM_table$Value, DSM_table$dem)
  • 13. There appears there is not meaningful. To fit a linear model, we can use the lm function: model1 <- lm(Value ~ dem, data=DSM_table, y=TRUE, x = TRUE) > model1 Call: lm(formula = Value ~ dem, data = DSM_table, x = TRUE, y = TRUE) Coefficients: (Intercept) dem1 0.715117 0.001856
  • 14. > summary(model1) Call: lm(formula = Value ~ dem1, data = DSM_table, x = TRUE, y = TRUE) Residuals: Min 1Q Median 3Q Max -3.917 -0.769 -0.224 0.389 48.895 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 7.151e-01 6.929e-02 10.32 <2e-16 *** dem1 1.856e-03 9.367e-05 19.82 <2e-16 *** --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 1.954 on 3300 degrees of freedom Multiple R-squared: 0.1063, Adjusted R-squared: 0.1061 F-statistic: 392.6 on 1 and 3300 DF, p-value: < 2.2e-16
  • 15. > class(model1) [1] "lm" the output from the lm function is an object of class lm. An object of class "lm" is a list containing at least the following components:
  • 16. > class(model1) [1] "lm" the output from the lm function is an object of class lm. An object of class "lm" is a list containing at least the following components: coefficients - a named vector of coefficients residuals - the residuals, that is response minus fitted values. fitted.values - the fitted mean values. rank - the numeric rank of the fitted linear model. weights - (only for weighted fits) the specified weights. df.residual -the residual degrees of freedom. call - the matched call. terms - the terms object used. contrasts - (only where relevant) the contrasts used. xlevels -(only where relevant) a record of the levels of the factors used in fitting. offset- the offset used (missing if none were used). y - if requested, the response used. x- if requested, the model matrix used. model - if requested (the default), the model frame used. na.action - (where relevant) information returned by model.frame on the special handling of NAs.
  • 17. class(model1) [1] "lm" model1$coefficients (Intercept) dem1 0.715116901 0.001856089 > formula(model1) Value ~ dem1 the output from the lm function is an object of class lm. An object of class "lm" is a list containing at least the following components:
  • 18. head(residuals(model1)) 1 2 3 4 5 6 6.8445691 -1.2674728 -0.7045624 -0.5960802 -1.1628119 -1.1990168 names(summary(model1)) [1] "call" "terms" "residuals" "coefficients" [5] "aliased" "sigma" "df" "r.squared" [9] "adj.r.squared" "fstatistic" "cov.unscaled" Here is a list of what is available from the summary function for this model:
  • 19. summary(model1)[[4]] Estimate Std. Error t value Pr(>|t|) (Intercept) 0.715116901 6.928677e-02 10.32112 1.333183e-24 dem1 0.001856089 9.367314e-05 19.81452 1.188533e-82 summary(model1)[[7]] [1] 2 3300 2 To extract some of the information from the summary which is of a list structure, we can use:
  • 20. > summary(model1)[["r.squared"]] [1] 0.1063245 > summary(model1)[[8]] [1] 0.1063245 What is the RSquared of model1?
  • 21. > summary(model1)[["r.squared"]] [1] 0.1063245 > summary(model1)[[8]] [1] 0.1063245 What is the RSquared of model1?
  • 22. head(predict(model1)) 1 2 3 4 5 6 5.034235 4.757678 3.022235 2.532228 2.502530 3.484401 head(DSM_table$Value) [1] 11.878804 3.490205 2.317673 1.936148 1.339719 2.285384 head(residuals(model1)) 1 2 3 4 5 6 6.8445691 -1.2674728 -0.7045624 -0.5960802 -1.1628119 -1.1990168 head(model2$residuals) 1 2 3 4 5 6 7.3395541 -1.1690560 -0.5363049 -1.2854938 -1.9692882 -1.5173124 > head(model2$fitted.values) 1 2 3 4 5 6 4.539250 4.659261 2.853978 3.221641 3.309007 3.802697
  • 23. plot(model1$y, model1$fitted.values) Lets plot() the observed vs. predicted from the model
  • 24. model2subset <-DSM_table2[, c("Value", "slp", "prec", "dem")] summary(model2subset) Value slp prec dem Min. : 0.000 Min. : 0.000 Min. : 424.5 Min. : 45.0 1st Qu.: 1.005 1st Qu.: 0.000 1st Qu.: 532.3 1st Qu.: 404.2 Median : 1.493 Median : 3.000 Median : 564.3 Median : 592.0 Mean : 1.912 Mean : 7.414 Mean : 597.5 Mean : 642.3 3rd Qu.: 2.244 3rd Qu.:11.000 3rd Qu.: 641.8 3rd Qu.: 768.0 Max. :50.332 Max. :56.000 Max. :1180.3 Max. :2375.0 NA's :1 We will regress SOC on Precipitation, Slope and Elevation. First lets subset these data out, then get their summary statistics
  • 25. cor(na.omit(model2subset)) Value slp prec dem Value 1.0000000 0.2730310 0.3155474 0.3317814 slp 0.2730310 1.0000000 0.5765489 0.6011170 prec 0.3155474 0.5765489 1.0000000 0.8158338 dem 0.3317814 0.6011170 0.8158338 1.0000000 A quick way to look for relationships between variables in a data frame is with the cor function. Note the use of the na.omit function.
  • 26. > pairs(na.omit(model2subset)) To visualize these relationships, we can use pairs
  • 27. > pairs(na.omit(model2subset)) To visualize these relationships, we can use pairs
  • 28. model2 <- lm(Value ~ slp + prec + dem, data = model2subset) model2 Call: lm(formula = Value ~ slp + prec + dem, data = model2subset) Coefficients: (Intercept) slp prec dem -0.027413 0.020314 0.001868 0.001048 fitting the multiple linear regression,
  • 29. summary(model2) Call: lm(formula = Value ~ slp + prec + dem, data = model2subset) Residuals: Min 1Q Median 3Q Max -3.527 -0.717 -0.219 0.379 48.907 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) -0.0274134 0.2240410 -0.122 0.902622 slp 0.0203135 0.0041948 4.843 1.34e-06 *** prec 0.0018682 0.0004956 3.770 0.000166 *** dem 0.0010477 0.0001681 6.232 5.18e-10 *** --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 1.937 on 3297 degrees of freedom (1 observation deleted due to missingness) Multiple R-squared: 0.1223, Adjusted R-squared: 0.1215 F-statistic: 153.2 on 3 and 3297 DF, p-value: < 2.2e-16
  • 30. TASK1: Regress SOC (Value) on; Slope (slp), Elevation (dem), TWI (twi), Annual Nightly Mean Temperature (tmpn), Annual Daily Mean Temperature (tmpd) TASK2: Share you results here, https://goo.gl/zlNcb5 Sample Data Sheet https://goo.gl/g5NQCv
  • 31. TASK1: Regress SOC (Value) on; Slope (slp), Elevation (dem), TWI (twi), Annual Nightly Mean Temperature (tmpn), Annual Daily Mean Temperature (tmpd) TASK2: Share your results here, https://goo.gl/zlNcb5 Sample Data https://goo.gl/g5NQCv