0

I looked at previous questions, and the solution was to use mblm(), which I did

library("tidyverse")
library("mblm")
ts_fit<-mblm(Qmelt~date,data=Qm)
ggplot(Qm,aes(x=date,y=as.numeric(Qmelt)))+geom_point(size=3,pch=7) + 
  geom_smooth(method="ts_fit",se=FALSE)

And I get this error.

Error in /.difftime((yy[j] - yy[i]), (xx[j] - xx[i])) : second argument of / cannot be a "difftime" object*

I tried doing some searches to fix this error but no dice. I'm not sure if it has something to do with the way my data is presented. I have two-week daily Qmelt data for each year from 2007 to 2016. For example, in 2007; I have data from May 22 to May 31. Then in 2008, I have data from May 14 to May 27, and so on...(here's the data output from R)

dput(Qm)  
structure(list(date = structure(c(13655, 13656, 13657, 13658, 
13659, 13660, 13661, 13662, 13663, 13664, 13665, 13666, 13667, 
13668, 14013, 14014, 14015, 14016, 14017, 14018, 14019, 14020, 
14021, 14022, 14023, 14024, 14025, 14026, 14360, 14361, 14362, 
14363, 14364, 14365, 14366, 14367, 14368, 14369, 14370, 14371, 
14372, 14373, 14711, 14712, 14713, 14714, 14715, 14716, 14717, 
14718, 14719, 14720, 14721, 14722, 14723, 14724, 15110, 15111, 
15112, 15113, 15114, 15115, 15116, 15117, 15118, 15119, 15120, 
15121, 15122, 15123, 15472, 15473, 15474, 15475, 15476, 15477, 
15478, 15479, 15480, 15481, 15482, 15483, 15484, 15485, 15838, 
15839, 15840, 15841, 15842, 15843, 15844, 15845, 15846, 15847, 
15848, 15849, 15850, 15851, 16185, 16186, 16187, 16188, 16189, 
16190, 16191, 16192, 16193, 16194, 16195, 16196, 16197, 16198, 
16546, 16547, 16548, 16549, 16550, 16551, 16552, 16553, 16554, 
16555, 16556, 16557, 16558, 16559, 16927, 16928, 16929, 16930, 
16931, 16932, 16933, 16934, 16935, 16936, 16937, 16938, 16939, 
16940), class = "Date"), Qmelt = c(0, 0, 0, 0, 0, 0, 0, 0, 7.65409, 
0, 0, 0, 0, 0, 1.20469, 2.16926, 2.08159, 2.17295, 2.93044, 2.96673, 
2.46754, 0, 2.45934, 3.8574, 0, 0, 0, 0, 1.1773, 1.92119, 2.32329, 
1.84254, 2.50648, 3.71239, 3.29142, 1.49057, 1.95089, 1.52618, 
2.51364, 2.30244, 4.09966, 0, 0, 0, 0, 1.46659, 0.828523, 1.65481, 
2.25438, 2.25799, 3.33945, 3.73636, 0, 0, 0, 2.08797, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.75658, 3.53432, 0, 10.5591, 
8.80988, 0, 8.69236, 9.68578, 0, 0, 0, 0, 0, 0, 2.03642, 3.2283, 
0, 2.22387, 3.5416, 3.91567, 2.78915, 2.09657, 3.36133, 5.20538, 
6.11938, 3.45213, 5.00769, 6.86979, 3.62911, 3.84688, 3.50385, 
3.32646, 2.33847, 4.93983, 4.7153, 0, 0, 0, 0, 0, 0, 0, 0, 1.71621, 
2.33383, 2.95071, 2.10973, 2.84552, 3.17072, 3.25686, 2.24731, 
0, 0, 0, 0, 0, 0, 4.30747, 0, 0, 11.0814, 10.6475, 12.0436, 0, 
0, 7.13739, 0, 0.713799, 1.81197, 2.40344)), row.names = c(NA, 
-140L), class = "data.frame")  

Any help would be appreciated. Apologize in advance, I'm still new to this coding work and using stack overflow syntax/do's and don'ts.

UseR10085
  • 7,120
  • 3
  • 24
  • 54
Bubs
  • 85
  • 1
  • 1
  • 11
  • 1
    What command actually gives that error? Where are your `ggplot` commands? It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Pictures of data cannot be copy/pasted into R. – MrFlick Apr 27 '20 at 03:33
  • Sorry when I got the error, I tested outside of `ggplot()` and I got the same error. But I forgot to put the ggplot code into the question. `ts_fit<-mblm(Qmelt~date,data=Qm)` and `geom_smooth(method="ts_fit",se=FALSE)` gave me that same error – Bubs Apr 27 '20 at 03:47
  • Don't put the data in image format. Use `dput(Qm)` in R and paste the output in the question. – UseR10085 Apr 27 '20 at 04:48
  • @BappaDas I've pasted the output into the question. I apologize that I didn't do it earlier. Will know next time! – Bubs Apr 27 '20 at 05:43

1 Answers1

0

You can convert the date into numeric and create a new column like

Qm$Date <- as.numeric(Qm$date)

Then use

library("tidyverse")
library("mblm")
ts_fit<-mblm(Qmelt ~ Date, Qm)
ggplot(Qm,aes(x=date,y=as.numeric(Qmelt)))+geom_point(size=3,pch=7)+     
  geom_abline(intercept = coef(ts_fit)[1], slope = coef(ts_fit)[2])
UseR10085
  • 7,120
  • 3
  • 24
  • 54
  • Thanks. That worked. Is there an r-squared value I can pull out like the `lm()` regression? – Bubs Apr 27 '20 at 20:49
  • The `mblm` does not have any predict function or implementation for r-squared calculation. You can use two things like `summary(ts_fit)` and `confint(ts_fit)` to have confidence intervals and summary for `mblm` model. Please also visit [this](https://stackoverflow.com/help/someone-answers) – UseR10085 Apr 28 '20 at 05:22