Documentation:Linear Regression Example With R

From UBC Wiki

Linear Regression using R

– read.table(“Filename” “,header=T) - read.table is an argument that will read a table from a text file. Put the full name of the file(directories included) into the Filename section. header=T will tell R that the first row of entrys is a header row.
> x
concentration absorb1 absorb2 absorb3 - This shows the table that was read from the file. In this example, the table is of concentrations vs. amount of a substance absorbed.
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(x2,x3,x4) - This line combines all the absorbed data from rows two, three, and four.
> 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(x1,3)) - This line combines all the data from the concentration. Rep will repeat the data, taking which set of data to repeat as it's first arguments, and how many times it should be repeated as it's second argument.
> 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) - This line of code combines aligns the data side by side into a 2-D array. so all concentrations match the given absorbed amount.
> 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)