Monthly Archives: September 2014

Linear Regression Example with R

Linear Regression using R

– read.table(“c:\\users\\wayne\\downloads\\d.txt”,sep=” “,header=T)
> x
concentration absorb1 absorb2 absorb3
1             0   0.016   0.019   0.004
2            10   0.101   0.128   0.118
3            20   0.199   0.216   0.212
4            30   0.352   0.356   0.348
5            40   0.524   0.522   0.534
6            50   0.625   0.648   0.694
7            60   0.701   0.712   0.705
> absorb = c(x[[2]],x[[3]],x[[4]])
> absorb
[1] 0.016 0.101 0.199 0.352 0.524 0.625 0.701 0.019 0.128 0.216 0.356 0.522
[13] 0.648 0.712 0.004 0.118 0.212 0.348 0.534 0.694 0.705
> concent = c(rep(x[[1]],3))
> concent
[1]  0 10 20 30 40 50 60  0 10 20 30 40 50 60  0 10 20 30 40 50 60
> xdata = data.frame(concent,absorb)
> xdata
concent absorb
1        0  0.016
2       10  0.101
3       20  0.199
4       30  0.352
5       40  0.524
6       50  0.625
7       60  0.701
8        0  0.019
9       10  0.128
10      20  0.216
11      30  0.356
12      40  0.522
13      50  0.648
14      60  0.712
15       0  0.004
16      10  0.118
17      20  0.212
18      30  0.348
19      40  0.534
20      50  0.694
21      60  0.705

The format of your table will be two columns listing the concentration in one column and in the second column absorbance values.  For other stats programs like Excel you will need to format your data in the spreadsheet like this.  And then produce a scatterplot and a fitted line.
> plot(concent,absorb)

plots data, x-axis is concentration and y-axis is absorbance

> lm.data = lm(absorb~concent)

Calculates linear regression model

> lines(concent,fitted(lm.data),col=”blue”)

Adds fitted line to scatterplot.

> summary(lm.data)

Summary of linear regression analysis

Call:
lm(formula = absorb ~ concent)

Residuals:
Min        1Q    Median        3Q       Max
-0.045119 -0.028119 -0.001952  0.023214  0.077381

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -0.004214   0.012909  -0.326    0.748
concent      0.012417   0.000358  34.681   <2e-16 ***

concent is the slope.  Intercept is constant


Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.03281 on 19 degrees of freedom
Multiple R-squared:  0.9844,    Adjusted R-squared:  0.9836
F-statistic:  1203 on 1 and 19 DF,  p-value: < 2.2e-16
*** significance, probabilty no relationship exists beteween absorbance and concentration.
Pr = probability this variable is no relevant

absorbance = 0.0124*concentration + -0.00421

Residuals = different between the actual values and the predicted values.

R-squared indicates correlation between y and x (absorbance and concentration)