1

gtrendsR の助けを借りて、いくつかの Google トレンド データをインポートし、それをプロットすることに成功しました。今、私は h.clust でデータをクラスター化しようとしていますが、私の問題は、距離ベクトルへの変換に成功していないデータ形式です。

データは次のようになります。

datestart, date-end, trend1, trend2, trend3 
1   2004-01-04 2004-01-10     57    18     39 
2   2004-01-11 2004-01-17     55    17     39 
3   2004-01-18 2004-01-24     56    20     43 
4   2004-01-25 2004-01-31     55    19     41 
5   2004-02-01 2004-02-07     57    20     39 
6   2004-02-08 2004-02-14     57    18     40 

データをプロットするために、data.frame を使用しました。

トレンドの「形」をさまざまなクラスターにクラスター化できるように、データを変換する方法のアイデアを手伝ってくれる人はいますか?

4

1 に答える 1

1

次のように、Gmail の傾向曲線の形状をまとめることができます。

set.seed(1)
library(gtrendsR)
library(dtw)

# Switch https://www.google.com/settings/security/lesssecureapps to on if needed:
gconnect("your_google_email", "your_google_psw")
cotton_trend <- gtrends(c("cotton", "satin", "velvet"), res="week")
d <- dist(t(cotton_trend$trend[, -(1:2)]), method="DTW")
hc <- hclust(d)

# plot the results
par(mfrow=c(1,2))
plot(cotton~end, cotton_trend$trend, type="l", ylim=range(cotton_trend$trend[, -(1:2)]), col=3, ylim="")
for (x in 4:ncol(cotton_trend$trend)) lines(x=cotton_trend$trend$end, y=cotton_trend$trend[, x], col=x)
legend("topleft", names(cotton_trend$trend)[-(1:2)], lty=1, col=3:ncol(cotton_trend$trend))
plot(hc)
rect.hclust(hc, k=2)

# extract clusters: 
cutree(hc, k=2)
# cotton  satin velvet 
#      1      2      1 

ここに画像の説明を入力

于 2016-02-05T15:21:59.487 に答える