Page 151 - Data Science Algorithms in a Week
P. 151
Regression
Weight prediction from height - linear
regression on real-world data
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)
[ 139 ]