Lm function, you can use a thread relationship to establish the development trend between two data, that is, thread fitting, and use the predict function to use the results of this development trend for data prediction.
fr = lm(Height~Weight,data=hw)coef(fr)
Establish a thread fitting method based on weight to predict the height. A straight line defined by intercept and slope. Visualized display:
library("ggplot2")ggplot(heights.weights,aes(x=Height,y=Weight)) + geom_point()+geom_smooth(method="lm")
This is an ideal situation.
Let's take a look at the prediction.
The predict function is based on the original model. Based on the results of the linear fitting model, weight is applied to get a predicted height.
predict(fr)
Well, let's put the predicted number together with the actual number.
heights.weights = transform(heights.weights,PredictedHeight = predict(fr))hw = melt(heights.weights, id.vars = c(‘Gender‘, ‘Height‘))ggplot(hw,aes(x=Height,y=value,color=variable)) + geom_point()
The graphical result is the same as the figure we saw previously. The predicted value is delayed in a straight line.
Data trend fitting-linear fitting