I'm using R. I have 25 variables over 15 time points, with 3 or more replicates per variable per time point. I've melt
ed this into a data.frame
, which I can plot happily using (amongst other things) ggplot's facet_wrap()
command. My melted data frame is called lis
; here's its head and tail, so you get an idea of the data:
> head(lis)
time variable value
1 10 SELL 8.170468
2 10 SELL 8.215892
3 10 SELL 8.214246
4 15 SELL 8.910654
5 15 SELL 7.928537
6 15 SELL 8.777784
> tail(lis)
time variable value
145 1 GAS5 10.92248
146 1 GAS5 11.37983
147 1 GAS5 10.95310
148 1 GAS5 11.60476
149 1 GAS5 11.69092
150 1 GAS5 11.70777
I can get a beautiful plot of all the time series, along with a fitted spline and 95% confidence intervals using the following ggplot2 commands:
p <- ggplot(lis, aes(x=time, y=value)) + facet_wrap(~variable)
p <- p + geom_point() + stat_smooth(method = "lm", formula = y ~ ns(x,3))
The trouble is that the smoother is not to my liking - the 95% confidence intervals are way off. I would like to use Gaussian Processes (GP) to get a better regression and estimate of covariance for my time series.
I can fit a GP using something like
library(tgp)
out <- bgp(X, Y, XX = seq(0, 200, length = 100))
which takes time X
, observations Y
and makes predictions at each point in XX
. The object out
contains a bunch of things about those predictions, including a covariance matrix I can use in place of the 95% confidence interval I get (I think?) from ns()
.
The trouble is I'm not how to wrap this function to make it interface with ggplot2::stat_smooth()
. Any ideas or pointers as to how to proceed would be greatly appreciated!
It looks like bgp
doesn't follow the standard R style for modelling functions. This means that you can't use it inside geom_smooth
and you'll need to fit the model outside of the ggplot2 call. You might also want to email the tgp
package author and encourage them to follow R standards.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With