我怎样才能将时间序列的指数衰减模型叠加到ggplot2图形中? [英] How can I overlay timeseries models for exponential decay into ggplot2 graphics?

查看:179
本文介绍了我怎样才能将时间序列的指数衰减模型叠加到ggplot2图形中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将指数衰减线(带有误差线)绘制在价格信息随时间变化的ggplot中的散点图上。我现在有这个:

  f2 < -  ggplot(data,aes(x = date,y = cost))+ 
geom_point(aes(y = cost),color =red,size = 2)+
geom_smooth(se = T,method =lm,formula = y〜x)+
#geom_smooth(se = T)+
theme_bw()+
xlab(Time)+
scale_y_log10(Price over time)+
opts(title =The随着时间的推移而降价)
print(f2)

关键是在geom_smooth命令, formula = y〜x 虽然这看起来像一个线性模型,但ggplot似乎会自动检测我的scale_y_log10并记录它。



现在,我的问题在于,日期是日期数据类型。我想我需要将它转换为t = 0以来的秒数,才能应用形式为 y = Ae ^ - (bx)的指数衰减模型。



我相信这是因为当我尝试诸如y = exp(x)之类的东西时,我收到一条消息,我认为(?)告诉我我不能带指数的日期。它读取:

lm.wfit中的错误(x,y,w,offset = offset,singular.ok = singular.ok,:

$ b

然而, b $ b NA / NaN / Inf在外部函数调用中(arg 1)
log(y)= x
可以正常工作(y是一个数字数据类型,x是日期。)

有没有一种方便的方法在geom_smooth(formula = formula)函数调用中的ggplot图中拟合指数增长/衰减时间序列模型?解析方案

但是我不知道如何用真实/杂乱的数据挑剔:

  set.seed(101) 
dat< - data.frame(d = seq.Date(as.Date(2010-01-01),
as.Date(2010-12-31),by = 1天),
y = rnorm(365,mean = exp(5-(1:365)/ 100),sd = 5))

library(ggplot2)
g1 < - ggplot(dat,aes(x = d,y = y))+ geom_point()+ expand_limits(y = 0)
g1 + geom_smooth(method =glm,family = gaussian =log),
start = c(5,0))


I'm trying to plot an exponential decay line (with error bars) onto a scatterplot in ggplot of price information over time. I currently have this:

f2 <- ggplot(data, aes(x=date, y=cost) ) +
    geom_point(aes(y = cost), colour="red", size=2) +
    geom_smooth(se=T, method="lm", formula=y~x) +
#   geom_smooth(se=T) +
    theme_bw() +
    xlab("Time") + 
    scale_y_log10("Price over time") +
    opts(title="The Falling Price over time")
print(f2)

The key line is in the geom_smooth command, of formula=y~x Although this looks like a linear model, ggplot seems to automatically detect my scale_y_log10 and log it.

Now, my issue here is that date is a date data type. I think I need to convert it to seconds since t=0 to be able to apply an exponential decay model of the form y = Ae^-(bx).

I believe this because when I tried things like y = exp(x), I get a message that I think(?) is telling me I can't take exponents of dates. It reads:

Error in lm.wfit(x, y, w, offset = offset, singular.ok = singular.ok, : NA/NaN/Inf in foreign function call (arg 1)

However, log(y) = x works correctly. (y is a numeric data type, x is a date.)

Is there a convenient way to fit exponential growth/decay time series models within ggplot plots in the geom_smooth(formula=formula) function call?

解决方案

This appears to work, although I don't know how finicky it will be with real/messy data:

set.seed(101)
dat <- data.frame(d=seq.Date(as.Date("2010-01-01"),
                         as.Date("2010-12-31"),by="1 day"),
                y=rnorm(365,mean=exp(5-(1:365)/100),sd=5))

library(ggplot2)
g1 <- ggplot(dat,aes(x=d,y=y))+geom_point()+expand_limits(y=0)
g1+geom_smooth(method="glm",family=gaussian(link="log"),
               start=c(5,0))

这篇关于我怎样才能将时间序列的指数衰减模型叠加到ggplot2图形中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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