library(quantmod)
## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(gtrendsR)
library(ggplot2)
library(lubridate)
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
getSymbols('LNC', src = 'yahoo', 
           from = Sys.Date() - years(5), to = Sys.Date())
## [1] "LNC"
LNC <- data.frame(
  LNC,
  date = as.Date(rownames(data.frame(LNC)))
)

# View the data
head(LNC)
##            LNC.Open LNC.High LNC.Low LNC.Close LNC.Volume LNC.Adjusted
## 2020-10-30    33.64    35.16   33.50     35.10    2721300     27.47414
## 2020-11-02    35.79    36.59   34.77     36.37    1870700     28.46821
## 2020-11-03    37.14    38.28   37.03     37.39    2133000     29.26661
## 2020-11-04    36.01    36.90   34.63     35.03    2770700     27.41934
## 2020-11-05    36.00    36.80   35.10     35.90    3318600     28.10033
## 2020-11-06    36.27    36.69   33.75     34.07    3075200     26.66792
##                  date
## 2020-10-30 2020-10-30
## 2020-11-02 2020-11-02
## 2020-11-03 2020-11-03
## 2020-11-04 2020-11-04
## 2020-11-05 2020-11-05
## 2020-11-06 2020-11-06
tail(LNC)
##            LNC.Open LNC.High LNC.Low LNC.Close LNC.Volume LNC.Adjusted
## 2025-10-22    39.72  39.9500  38.940     39.42    1209800        39.42
## 2025-10-23    39.32  39.7400  39.270     39.35     893600        39.35
## 2025-10-24    39.65  40.3300  39.650     39.93     834900        39.93
## 2025-10-27    40.34  40.6200  39.760     40.32    1226700        40.32
## 2025-10-28    40.36  40.3600  39.670     40.12    1329400        40.12
## 2025-10-29    39.95  40.6612  39.595     40.03    1425187        40.03
##                  date
## 2025-10-22 2025-10-22
## 2025-10-23 2025-10-23
## 2025-10-24 2025-10-24
## 2025-10-27 2025-10-27
## 2025-10-28 2025-10-28
## 2025-10-29 2025-10-29
ggplot(LNC, aes(date)) + 
  geom_line(aes(y = LNC.Adjusted))

ggplot(LNC, aes(date)) + 
  geom_line(aes(y = LNC.Close))

#now let's predict Lincoln Financials stock price with Google Trends search queries  
library(gtrendsR)
trends.dat <- gtrends('AI', 'US')$interest_over_time

# View the data
head(trends.dat)
##         date hits keyword geo      time gprop category
## 1 2020-10-25    2      AI  US today+5-y   web        0
## 2 2020-11-01    2      AI  US today+5-y   web        0
## 3 2020-11-08    2      AI  US today+5-y   web        0
## 4 2020-11-15    2      AI  US today+5-y   web        0
## 5 2020-11-22    2      AI  US today+5-y   web        0
## 6 2020-11-29    3      AI  US today+5-y   web        0
tail(trends.dat)
##           date hits keyword geo      time gprop category
## 257 2025-09-21   83      AI  US today+5-y   web        0
## 258 2025-09-28   81      AI  US today+5-y   web        0
## 259 2025-10-05   77      AI  US today+5-y   web        0
## 260 2025-10-12   72      AI  US today+5-y   web        0
## 261 2025-10-19   67      AI  US today+5-y   web        0
## 262 2025-10-26   70      AI  US today+5-y   web        0
p <- ggplot(trends.dat, aes(date)) + 
  geom_line(aes(y = hits))
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
ggplotly(p)