我想使用r中的gtrends软件包下载多个关键字的每日Google搜索数据。我需要2004-18年之间30个关键字的搜索数据。由于Google一次只能提取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
答案 0 :(得分: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