Data trend fitting-linear fitting

Source: Internet
Author: User
Tags ggplot

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.