码迷,mamicode.com
首页 > 其他好文 > 详细

数据趋势拟合--线性拟合

时间:2014-10-21 17:56:58      阅读:615      评论:0      收藏:0      [点我收藏+]

标签:http   color   os   ar   for   sp   数据   2014   ad   

lm函数,可以用一种线程关系建立两个数据之间的发展趋势,也就是线程拟合,并用predict函数将这种发展趋势的结果用于数据的预测。

fr = lm(Height~Weight,data=hw)
coef(fr)

bubuko.com,布布扣

建立基于Weight来预测Height的线程拟合。由截距和斜率定义的一条直线。可视化如下图示:

library("ggplot2")
ggplot(heights.weights,aes(x=Height,y=Weight)) + geom_point()+geom_smooth(method="lm")

bubuko.com,布布扣

这是一种很理想的情况。

那我们看一下预测。

predict函数是在原模型的基础上,依据线性拟合模型的结果对所有的Height,应用Weight得到一个预测的Height。

predict(fr)

bubuko.com,布布扣

嗯,让我们把预测的数和真实的数放到一起。

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()

图形化的结果和我们之前看到的图是一样的。预测的值就是延着直线前进的。

bubuko.com,布布扣


数据趋势拟合--线性拟合

标签:http   color   os   ar   for   sp   数据   2014   ad   

原文地址:http://my.oschina.net/piorcn/blog/335854

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!