| Example:
Consider the following Hooke's law experiment:
|
Hooke's law apparatus |
Different known weights (w) are hung from the spring and the
corresponding lengths (L) are measured. Here is the data
w kg | L cm |
0.5 | 31.3 |
1.0 | 31.9 |
1.5 | 33.0 |
2.0 | 34.2 |
2.5 | 35.1 |
3.0 | 36.1 |
3.5 | 36.8 |
4.0 | 37.9 |
4.5 | 39.0 |
5.0 | 40.1 |
For your convenience the same data set has been stored in the file spring.dat. To statistically analyse this data set
using R we have to first read the data into R:
hook <- as.matrix(read.table("spring.dat"))
Now hook is a 10 by 2 matrix, the first column stores w
and the second column stores L. Let us store the columns in two
separate variables:
w <- hook[,1]
L <- hook[,2]
Now make a scatterplot of L versus w:
plot(w,L)
Notice that this plot consists of 10 points, one point for each
(w,L) pair. The points lie approximately on a straight
line. It is this linear pattern that first led Hooke to postulate
his famous law:
L = a + b w,
where a and b are some constants depending on the nature of
the spring. In particular, b is called Young's modulus of
the spring. Let us try to estimate Young's modulus of our spring based on
the given data. For this we have to fit a straight line to the
scatterplot.
Note that due to experimental errors, the
points do not lie exactly on a straight line. To fit a straight line
to the scatterplot we shall use the lm command:
val <- lm(L ~ w)
val$coeff
Notice the expression L ~ w carefully. The ~ character
is called a tilde and can be found on the keyboard just below the
Esc in the top, left hand corner. The expression L ~ w is
called a formula in the R language. It stands for the straight line
L = a + b w.
The command lm(L ~ w) asked R to estimate the coefficients
a and b from the data set so that the line passes through
all the points as nearly as possible. The command val$coeff asks
R to print the values of the coefficients, i.e., the estimated
values of a and b. The output of R looks something like:
(Intercept) w
30.133333 1.966061
This says that a equals 30.133333 and b equals 1.966061.
Thus, our spring has Young's modulus equal to 1.966061 cm/kg.
The line L = a + b w for these a and
b should approximately pass through all the points in the
scatterplot? Check this by plotting the line on the scatterplot:
plot(w,L)
abline(val$coeff)
What will be the length of the spring if you hang weights 2.1 kg or 2.4 kg
or 3.7 kg? You can compute the lengths by directly using the formula
L = a + b w.
But R can do that for you as follows. First make a list of all the values of
w for which you want to predict the lengths:
newWs <- data.frame(w=c(2.1,2.3,3.7))
Now use the predict command:
predict(val,newWs)
|