【问题标题】:Loop to download Google search data for multiple keywords using gtrends in r在 r 中使用 gtrends 循环下载多个关键字的 Google 搜索数据
【发布时间】:2020-02-28 23:51:22
【问题描述】:

我想使用 r 中的 gtrends 包下载多个关键字的每日 Google 搜索数据。我需要 2004-18 年间 30 个关键字的搜索数据。由于谷歌一次只允许提取 9 个月的每日数据,我必须为每个关键字一次下载 6 个月的数据。我还对 6 个月的数据进行了一些额外的计算(参见下面的代码)。 一次下载6个月的数据后,我想将数据组合成一个时间序列。之后,我想省略 NA,在工作日假人上回归并保留残差,最后按时间序列自己的标准差缩放时间序列。最后,我想将调整后的数据保存为带有搜索词名称的向量(参见下面的代码)。

如何创建一个循环,分别对每个搜索词进行搜索和计算,并将调整后的数据保存为向量?我尝试使用不同类型的循环和应用函数,但不明白如何将它们与 gtrends 包一起使用。

#define the keywords
keywords=c("Charity")

#set the geographic area: GB = Great Britain
country=c('GB')

#timeframe
time=("2004-01-01 2004-06-30")
#set channels 
channel='web'
trends = gtrends(keywords, gprop =channel,geo=country, time = time )
#select only interest over time 
time_trend=trends$interest_over_time
time_trend$hits[time_trend$hits=="0"]<-1
time_trend$change <- c(NA,diff(log(time_trend$hits)))
set1=time_trend[which(weekdays(as.Date(time_trend$date, format = "%m/%d/%Y"))
                 %in% c('Monday','Tuesday', 'Wednesday', 'Thursday', 'Friday')), ]

这一直持续到 set30,之后:

### Combine each 6 month data set ####

set <- rbind(set1,..,set30)

#omit NAs from the set
set <- na.omit(set)

# Regress on weekday and month dummies and keep the residual
set$weekday <- weekdays(set$date) #dummy for weekdays
weekday <- set$weekday

setti$month <- months(setti$date) #dummy for months
month <- set$month
mod <- lm(set$change~month+weekday)

#keep the residuals after the regression
set$residuals <- residuals(mod)

# Scale each by the time-series standard deviation #
sd <- sd(set$residuals)
set$adj_residuals=((set$residuals)/(sd))
adj_svi <- set$adj_residuals

# Save the deseasonalized and standardized ln daily change in keyword search volume as a vector

charity <- adj_svi

【问题讨论】:

    标签: r loops gtrendsr


    【解决方案1】:

    你可以用 lappy 和一个定义的函数来做到这一点

    search6m=function(keywords,channel=channel,country=country,time=time){
      trends = gtrends(keywords, gprop =channel,geo=country, time = time )
    #select only interest over time 
    time_trend=trends$interest_over_time
    time_trend$hits[time_trend$hits=="0"]<-1
    time_trend$change <- c(NA,diff(log(time_trend$hits)))
    set1=time_trend[which(weekdays(as.Date(time_trend$date, format = "%m/%d/%Y"))
                     %in% c('Monday','Tuesday', 'Wednesday', 'Thursday', 'Friday')), ]]
    set1
    }
    
    # difine time search intervals 
    stime="2004-01-02"
    etime="2005-12-31"
    times=seq.Date(as.Date(stime),as.Date(etime),by="6 months")
    tims=sapply(1:(length(times)-1),function(z)paste(times[z],times[z+1],sep=" "))
    # get data for each interval and use rbind to combine
    set <- lapply(tims,function(zt)search6m(keywords,channel,country,time=zt))
    set = do.call("rbind",set)
    
    # do all the rest of your code
    

    【讨论】:

    • 这行不通,因为我还必须为每个关键字分别执行其余代码。简而言之,我需要一种方法来为 30 个不同的关键字运行我的整个代码,并为每个关键字提供一个向量 "keywordname"
    • 当然这不是最终的答案!您是否尝试过使用包含多个单词的关键字来测试代码?尝试一下,您将获得集合中所有关键字的所有数据。然后你需要通过 set$keyword 进行迭代来完成剩下的工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-28
    • 2013-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多