Here we predict the weight of a man from his height using linear regression from the following data in the table for men:
Height in cm |
Weight in kg |
180 |
75 |
174 |
71 |
184 |
83 |
168 |
63 |
178 |
70 |
172 |
? |
We would like to estimate the weight of a man given that his height is 172cm.
Analysis using R:
In the previous example Fahrenheit and Celsius conversion, the data fitted the linear model perfectly. Thus we could perform even a simple mathematical analysis (solving basic equations) to gain the conversion formula. Most of the data in the realworld does not fit a model perfectly. For such an analysis, it is good to find the model that fits the given data with the minimal error. We use R do find such a linear model.
Input:
We put the data from the table above into the vectors and try to fit the linear model.
# source_code/6/weight_prediction.r
men = data.frame( height = c(180,174,184,168,178), weight = c(75,71,83,63,70) ) model = lm(weight ~ height, data = men)
print(model)
Output:
$ Rscript weight_prediction.r
Call: lm(formula = weight ~ height, data = men) Coefficients: (Intercept) height -127.688 1.132
Thus the formula expressing the linear relationship between the weight and the height is as follows: weight=1.132*height-127.688. Therefore, we estimate that the man with the height of 172cm would have the weight 1.132*172-127.688=67.016 kg.