podatki <- read.table("~/R/Jan/PiB.csv", header=TRUE,, sep=";", dec=".")

podatki$dan <- seq(from=1, to=length(podatki$Vrednost))
head(podatki)
##        Datum Vrednost dan
## 1 11.04.2021   274.99   1
## 2 12.04.2021   280.95   2
## 3 13.04.2021   295.59   3
## 4 14.04.2021   298.09   4
## 5 15.04.2021   319.18   5
## 6 16.04.2021   329.26   6
ggplot(podatki, aes(x=dan, y=Vrednost)) +
  geom_line() +
  geom_point() + 
  ylab("Vrednost PiB")

podatki$logVrednost <- log(podatki$Vrednost, base=10)
head(podatki)
##        Datum Vrednost dan logVrednost
## 1 11.04.2021   274.99   1    2.439317
## 2 12.04.2021   280.95   2    2.448629
## 3 13.04.2021   295.59   3    2.470690
## 4 14.04.2021   298.09   4    2.474347
## 5 15.04.2021   319.18   5    2.504036
## 6 16.04.2021   329.26   6    2.517539
fit_lin <- lm(Vrednost ~ dan, 
          data = podatki)

summary(fit_lin)
## 
## Call:
## lm(formula = Vrednost ~ dan, data = podatki)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -3064.3 -1987.1  -292.6  1779.8  4069.5 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -3823.77     582.05   -6.57 1.91e-08 ***
## dan           322.58      17.46   18.48  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2168 on 55 degrees of freedom
## Multiple R-squared:  0.8613, Adjusted R-squared:  0.8587 
## F-statistic: 341.4 on 1 and 55 DF,  p-value: < 2.2e-16

Vsak dan se vrednost linearnega trenda poveča za 322,6 PiB. R2 linearna funkcija je 0,86

fit_exp <- lm(logVrednost ~ dan, 
           data = podatki)

summary(fit_exp)
## 
## Call:
## lm(formula = logVrednost ~ dan, data = podatki)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.16645 -0.05398  0.01769  0.05044  0.09345 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 2.3523534  0.0178285  131.94   <2e-16 ***
## dan         0.0365273  0.0005347   68.31   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.06642 on 55 degrees of freedom
## Multiple R-squared:  0.9884, Adjusted R-squared:  0.9881 
## F-statistic:  4666 on 1 and 55 DF,  p-value: < 2.2e-16
b0 <- 10^(summary(fit_exp)$coefficients[1])
b0
## [1] 225.0885
b1 <- 10^(summary(fit_exp)$coefficients[2])
b1
## [1] 1.087745

Vsak dan se vrednost eksponentnega trenda PiB poveča za 8,8 %.

podatki$Napoved = b0*b1^podatki$dan
podatki$e2 = (podatki$Vrednost - podatki$Napoved)^2
head(podatki)
##        Datum Vrednost dan logVrednost  Napoved         e2
## 1 11.04.2021   274.99   1    2.439317 244.8390  909.08158
## 2 12.04.2021   280.95   2    2.448629 266.3225  213.96288
## 3 13.04.2021   295.59   3    2.470690 289.6911   34.79679
## 4 14.04.2021   298.09   4    2.474347 315.1102  289.68711
## 5 15.04.2021   319.18   5    2.504036 342.7597  556.00145
## 6 16.04.2021   329.26   6    2.517539 372.8353 1898.80548
ke = sum(podatki$e2)
ke
## [1] 202395447
ky = var(podatki$Vrednost) * (length(podatki$Vrednost) - 1)
ky
## [1] 1863962777
r2 = 1 - (ke/ky)
r2
## [1] 0.8914166

R2 eksponentna funkcija 0,89.

napovedi_lin <- data.frame(dan = c(58:100))
napovedi_lin$Vrednosti <- predict(fit_lin, newdata = napovedi_lin)

fit_exp <- lm(log(Vrednost) ~ dan, 
           data = podatki)

napovedi_exp <- data.frame(dan = c(58:100))
napovedi_exp$lnVrednosti <- predict(fit_exp, newdata = napovedi_exp)
napovedi_exp$Vrednosti <- exp(napovedi_exp$lnVrednosti)




ggplot(podatki, aes(x=dan, y=Vrednost)) +
  theme_linedraw() +
  geom_point() +
  scale_x_continuous(limits = c(1, 80), breaks = seq(from = 1, to = 80, by = 1)) +
  ylab("PiB") +
  geom_function(fun = function(x) (coef(fit_lin)[1]) + (coef(fit_lin)[2])*(x), col="black", lty=1, size=1) +
  geom_function(fun = function(x) exp(coef(fit_exp)[1]) * (exp(coef(fit_exp)[2])^(x)), col="black", lty=2, size=1) +
  geom_vline(xintercept = 57, size=1)