# R - Calculate trendline and extend it for 365 days automatically

I have daily data with logarithmic decreasing trend (around 30-days-data). In excel I'm calculating these data's trendline and with that trendline equation, I'm projecting these values to 365-days data. Then find the sumation of these 365 days' values.

Is there any ways to automize that in R by writing a function or using any package ?

Thanks.

• I don't really understand what you are trying to do. But is it estimating the coefficient of a 30-day trend, then you can regress your data against a 30-day sequence, t<-seq(1,30), and then estimate the regression by $y=\beta_0+\beta_1 t^{-1} + \beta_2 x +\epsilon$ – fredrikhs Aug 16 '13 at 7:25
• After I calculate the trend equation as shown in the picture, I would like to find all values with respect to that equation, by writing 31,32,33,...,364,365 into the 'x' and sum all these values. – CanCeylan Aug 16 '13 at 7:27

I understand you want to estimate the coefficient of the logarithm of a time trend, and predict the variable 335 days ahead after this. Your variable is $Y$ and the time trend is given by t<-seq(1,30), estimate

Y.lm<-lm(Y~log(t))

Y.predict<-predict(Y.lm, nstep=335)

is this what you want to do?

EDIT:

You can re-estimate the regression every time you have a new datapoint, extract the coefficient (from the intercept and time trend) and run the loop again. # day number n.

t <- seq(1,n)

x.lm<-lm(x~log(t))

beta_0<-coef(x.lm)

beta_1<-coef(x.lm)

y <- rep(0,n)

for (i in 1:n){

y[i]<-beta_0+beta_1*log(i) }

• Thanks for your answer. I think your solution is correct, but I couldn't apply it to my problem. How should I assign the variable Y ? I'm reading my data with t <- scan("data.dat"). So that's my sequence, but what will be the predicted variable ? – CanCeylan Aug 16 '13 at 7:57
• Is data.dat your dataset? Then you can just scan and call it Y or X or whatever. Or just import it to your workspace. But t is a time trend, so it has to be a sequence, t<-seq(1,30). You are regressing your dependent variable against a time trend to find the relationship between them. The predicted variable will be your independent variable, which you are scanning in data.dat. Right? – fredrikhs Aug 16 '13 at 8:04
• I understand, thank you very much ! But I have, Error in UseMethod("predict") : no applicable method for 'predict' applied to an object of class "c('double', 'numeric')" – CanCeylan Aug 16 '13 at 8:53
• I just edited my answer, it's supposed to be predict(Y.lm...) did you try that? Sorry for the mistake. – fredrikhs Aug 16 '13 at 8:57
• Thanks @fredrikhs, I tried it. One last question, when I printout the Y.predict, it only returns values till 30. How should I get all values until 365 ? – CanCeylan Aug 16 '13 at 10:36