绘制最合适的线R [英] Plot a best fit line R

查看:105
本文介绍了绘制最合适的线R的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我有一个很大的数据集,温度一直在上升和下降. 我想对数据进行平滑处理,并在所有温度下绘制最佳拟合线,

Right now i have a large data set with temperature going up and down all the time. I want to smoothen my data and plot the best fit line with all the temperature,

以下是数据:

weather.data  
    date        mtemp   
1   2008-01-01  12.9        
2   2008-01-02  12.9        
3   2008-01-03  14.5        
4   2008-01-04  15.7            
5   2008-01-05  17.0        
6   2008-01-06  17.8    
7   2008-01-07  20.2        
8   2008-01-08  20.8        
9   2008-01-09  21.4        
10  2008-01-10  20.8        
11  2008-01-11  21.4        
12  2008-01-12  22.0        

以此类推......直到2009年12月31日

and so on............... til 2009 Dec 31

我当前的图形如下所示,并且我的数据适合于回归,例如运行平均值或黄土:

My current graph looks like this and my data fit a regression like either the running average or loess:

但是,当我尝试将其与移动平均线拟合时,它变成这样:

However, when I tried to fit it with the running average, it became like this:

这是我的代码.

plot(weather.data$date,weather.data$mtemp,ylim=c(0,30),type='l',col="orange")
par(new=TRUE)

有人可以帮我吗?

推荐答案

取决于您的实际数据以及如何对其进行平滑处理,以及为什么要对其进行平滑处理,有多种选择.

Depending on your actual data and how you want to smooth it, and why you want to smooth it there are various options.

我正在向您展示线性回归(一阶和二阶)和局部回归(LOESS)的示例.这些可能不是用于数据的良好统计模型,但是如果不看就很难分辨.无论如何:

I am showing you examples with linear regression (first and second order) and local regression (LOESS). These may or may not be the good statistical models to use for your data, but it is difficult to tell without seeing it. In any case:

time <- 0:100
temp <- 20+ 0.01 * time^2 + 0.8 * time + rnorm(101, 0, 5)

# Generate first order linear model
lin.mod <- lm(temp~time)

# Generate second order linear model
lin.mod2 <- lm(temp~I(time^2)+time)

# Calculate local regression
ls <- loess(temp~time)

# Predict the data (passing only the model runs the prediction 
# on the data points used to generate the model itself)
pr.lm <- predict(lin.mod)
pr.lm2 <- predict(lin.mod2)
pr.loess <- predict(ls)

par(mfrow=c(2,2))
plot(time, temp, "l", las=1, xlab="Time", ylab="Temperature")
lines(pr.lm~time, col="blue", lwd=2)

plot(time, temp, "l", las=1, xlab="Time", ylab="Temperature")
lines(pr.lm2~time, col="green", lwd=2)

plot(time, temp, "l", las=1, xlab="Time", ylab="Temperature")
lines(pr.loess~time, col="red", lwd=2)

另一种选择是使用移动平均线.

Another option would be to use a moving average.

例如:

library(zoo)
mov.avg <- rollmean(temp, 5, fill=NA)
plot(time, temp, "l")
lines(time, mov.avg, col="orange", lwd=2)

这篇关于绘制最合适的线R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆