R 在ggplot2中绘制时间序列线性回归
我的问题是关于用ggplot2表示tslm的时间序列分析 我使用预测包分解了地中海海温时间序列的趋势、季节和剩余分量。然后我用tslm寻找了趋势分量的线性回归的斜率趋势。但我不知道如何用ggplot2绘制tslm。我应该用geom_smoothmodel=lm绘制SST趋势分量吗?lm会提供与tslm相同的结果斜率吗 这是用于构建和分解SST时间序列的代码R 在ggplot2中绘制时间序列线性回归,r,ggplot2,time-series,linear-regression,R,Ggplot2,Time Series,Linear Regression,我的问题是关于用ggplot2表示tslm的时间序列分析 我使用预测包分解了地中海海温时间序列的趋势、季节和剩余分量。然后我用tslm寻找了趋势分量的线性回归的斜率趋势。但我不知道如何用ggplot2绘制tslm。我应该用geom_smoothmodel=lm绘制SST趋势分量吗?lm会提供与tslm相同的结果斜率吗 这是用于构建和分解SST时间序列的代码 library(forecast) # Loop to calculate trend for any grid point/column
library(forecast)
# Loop to calculate trend for any grid point/column
for (i in 2:length(data)){
# read variable/column to analyse
var<-paste("V",i,sep="")
ff<-data$fecha
valor<-data[,i]
datos2<-as.data.frame(cbind(data$fecha,valor))
#Build time series
datos.ts<-ts(datos2$valor, frequency = 365)
datos.stl <- stl(datos.ts,s.window = 365)
# tslm: Save trend component
datos.tslm<-tslm(datos.ts ~ trend)
output[,i-1]<-datos.stl$time.series[,2]
}
# Summarize trends for the whole Mediterranean (mean value to be plotted)
trend<-as.data.frame(rowMeans(output[,1:length(output)]))
以及用geom_平滑绘制的代码
trend.plot<-ggplot(data=trend, aes(x=fecha, y=trend)) + geom_point(size=0.1) +
geom_smooth(method='lm', data = trend[1:12784,])
编辑1
由于SST数据由一堆文件组成,我已将趋势数据上传到Dropbox,并在本文件中提供。我试图理解您的问题,作为第一次尝试,我修改了您的代码,因为所附数据仅包含2列,因此我删除了for循环,但概括应该不难
library(forecast)
library(ggplot2)
library(zoo)
data <- read.csv('../Downloads/trend_data.csv', header=TRUE)
data$fecha <- as.Date(data$fecha)
i <- 2
# read variable/column to analyse
var<-paste("V",i,sep="")
ff<-data$fecha
valor<-data[,i]
datos2<-as.data.frame(cbind(data$fecha,valor))
#Build time series
datos.ts<-ts(datos2$valor, frequency = 365)
datos.stl <- stl(datos.ts,s.window = 365)
# tslm: Save trend component
datos.tslm<-tslm(datos.ts ~ trend)
output <-datos.stl$time.series[,2]
# Summarize trends for the whole Mediterranean (mean value to be plotted)
# trend<-as.data.frame(rowMeans(output[,1:length(output)]))
ggplot(data=data, aes(x=fecha, y=trend)) + geom_point(size=0.1) +
geom_smooth(method='lm', data = data.frame(fecha=data$fecha, trend=output), aes(x=fecha, y=output))
如果你想要趋势的平滑版本
ggplot(data=data, aes(x=fecha, y=trend)) + geom_point(size=0.1, col="red") +
geom_smooth(data = data.frame(fecha=data$fecha, trend=output), aes(x=fecha, y=output),col="blue",size=0.1)
您提供的数据以每天一个点的线图形式打印。这能解决你的问题吗
library(dplyr)
library(ggplot2)
trend_data <- read.csv2("../trend_data.csv",
sep = ",",stringsAsFactors = FALSE)
df <- trend_data %>% mutate(fecha = as.Date(fecha), trend = as.numeric(trend))
ggplot(df, aes(x = fecha, y = trend)) +
geom_line() +
geom_point()
您是否尝试过method=tslm?它可能会提供一个类似的输出with@RobinGertenbach它不起作用,因为tslm适用于时间序列,而不是趋势。它给出了这样一条错误消息:stat_smooth中的计算失败:不是时间序列数据,使用lm我尝试运行您的代码。你能定义for循环开头的数据是什么吗?我支持这个动作,你能添加一个脚本为我们创建一些数据吗?有很多这样做的例子。它会给我们一些重要的参数,比如变量的数量和每个变量的观察数量……你没有回答我的问题。当我运行您的示例代码时,我得到一个错误,当我执行输出[,I-1]=…]时,输出尚未定义。。。。缺少什么?嗨,由于为循环提供了数据,所以不需要。我提供了要绘制的数据。实际上,我的问题是关于绘制tslm输出。for循环使用tslm进行线性回归,但ggplot只知道lm,并且在绘图时tslm和lm之间的结果略有不同。tslm适用于时间序列中的线性回归。Hi@wadwas,这只绘制时间序列,我可以这样做。如果可能的话,我想将tslm应用为geom_平滑。您希望geom_平滑基于哪列?
library(dplyr)
library(ggplot2)
trend_data <- read.csv2("../trend_data.csv",
sep = ",",stringsAsFactors = FALSE)
df <- trend_data %>% mutate(fecha = as.Date(fecha), trend = as.numeric(trend))
ggplot(df, aes(x = fecha, y = trend)) +
geom_line() +
geom_point()