0

我想使用 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
4

1 回答 1

0

您可以使用 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
于 2019-11-03T14:08:17.367 回答