1

製品の売上の推移を約 1 秒で説明する R データフレームがあります。四半期ベースの 2000 ショップ、5 つの列 (つまり、5 つの期間)。Rで解析する方法を知りたいです。

私はすでにいくつかの基本的な分析を試みました。つまり、第 1 期間、第 2 期間などの平均売上を決定し、次に各期間の平均を決定し、各ショップの進化を相対的に比較します。この一般的な進化。例えば、1期50000枚、5期35000枚の売上があるので、各店の5期の通常の売上は、35/55=0.63×1期の金額になると思います。期間の販売: X 店が第 1 期間に 100 個の商品を販売した場合、通常、第 5 期間に 63 個の商品を販売する必要があると想定します。

明らかに、これは簡単に実行できる方法ですが、統計的に適切ではありません。

R 2 乗を最小化するトレンド カーブを決定できる方法が必要です。私の目的は、一般的な傾向を中和することによってショップの売上を分析できるようにすることです。統計的に正しいアプローチを使用して、パフォーマンスの低いショップとパフォーマンスの高いショップを正確に知りたい.

私のデータフレームはこのように構成されています:

shopID | sum | qt1 | qt2 | qt3 | qt4 | qt5
000001 | 150 | 45  | 15  | 40  | 25  | 25
000002 | 100 | 20  | 20  | 20  | 20  | 20
000003 | 500 | 200 | 0   | 100 | 100 | 100
... (2200 rows)

次の機能を使用して、timeseries をリストに入れようとしましたが、成功しました。

reversesales=t(data.frame(sales$qt1,sales$qt2,sales$qt3,sales$qt4,sales$qt5))
# I reverse rows and columns of the frame in order that the time periods be the rows
timeser<-ts(reversesales,start=1,end=5, deltat=1/4)
# deltat=1/4 because it is a quarterly basis, 1 and 5 because I have 5 quarters

それでも、この変数では何もできません。2200行あるため、(「プロット」関数を使用して)プロットを実行できません(したがって、Rは2200個の連続したプロットを作成したいと考えています。明らかにこれは私が望んでいるものではありません)。

また、店舗ごとの各期間の売上の理論トレンドと理論値の求め方がわからない

ご協力ありがとうございました!(そしてメリークリスマス)

4

2 に答える 2

1

混合モデルの実装:

install.packages("nlme")
library("nlme")
library(dplyr)

# Generating some data with a structure like yours:
start <- round(sample(10:100, 50, replace = TRUE)*runif(50))
df <- data_frame(shopID = 1:50, qt1 = start, qt2 =round(qt1*runif(50, .5, 2)) ,qt3 = round(qt2*runif(50, .5, 2)), qt4 = round(qt3*runif(50, .5, 2)), qt5 = round(qt4*runif(50, .5, 2)))
df <- as.data.frame(df)

# Converting in into the long format:
df <- reshape(df, idvar = "shopID", varying = names(df)[-1], direction = "long", sep = "")

Estimating the model:
mod <- lme(qt ~ time, random = ~ time | shopID, data = df)

# Extract the random effects for comparison:
random.effects(mod)
(Intercept)        time
1   74.0790805   3.7034172
2    7.8713699   4.2138001
3   -8.0670810  -5.8754060
4  -16.5114428  16.4920663
5  -16.7098229   6.4685228
6  -11.9630688  -8.0411504
7  -12.9669777  21.3071366
8  -24.1099280  32.9274361
9    8.5107335  -9.7976905
10 -13.2707679  -6.6028927
11   3.6206163  -4.1017784
12  21.2342886  -6.7120725
13 -14.6489512  11.6847109
14 -14.7291647   2.1365768
15  10.6791941   3.2097199
16 -14.1524187  -1.6933291
17   5.2120647   8.0119320
18  -2.5172933  -6.5011416
19  -9.0094366  -5.6031271
20   1.4857512  -5.9913865
21 -16.5973442   3.5164298
22 -26.7724763  27.9264081
23  49.0764631 -12.9800871
24  -0.1512509   2.3589947
25  15.7723150  -7.9295698
26   2.1955489  11.0318875
27  -8.0890346  -5.4145977
28   0.1338790  -8.3551182
29   9.7113758  -9.5799588
30  -6.0257683  42.3140432
31 -15.7655545  -8.6226255
32  -4.1450984  18.7995079
33   4.1510104  -1.6384103
34   2.5107652  -2.0871890
35 -23.8640815   7.6680185
36 -10.8228653  -7.7370976
37 -14.1253093  -8.1738468
38  42.4114024  -9.0436585
39 -10.7453627   2.4590883
40 -12.0947901  -5.2763010
41  -7.6578305  -7.9630013
42 -14.9985612  -0.4848326
43 -13.4081771  -7.2655456
44 -11.5646620  -7.5365387
45   6.9116844 -10.5200339
46  70.7785492 -11.5522014
47  -7.3556367  -8.3946072
48  27.3830419  -6.9049164
49  14.3188079  -9.9334156
50 -15.2077850  -7.9161690

私は値を次のように解釈します: それらをゼロからの偏差と見なして、正の値は平均からの正の偏差であるのに対し、負の値は平均からの負の偏差です。以下で確認されるように、2 つの列の平均はゼロです。

round(apply(random.effects(mod), 2, mean))
(Intercept)        time 
0           0 
于 2014-12-27T23:29:20.960 に答える
1
library(zoo)

#Reconstructing the data with four quarter columns (instead of five quarters as in your example)

shopID <- c(1, 2, 3, 4, 5)
sum <- c(150, 100, 500, 350, 50) 
qt1 <- c(40, 10, 130, 50, 10)
qt2 <- c(40, 40, 110, 100, 15)
qt3 <- c(50, 30, 140, 150, 10)
qt4 <- c(20, 20, 120, 50, 15)
myDF <- data.frame(shopID, sum, qt1, qt2, qt3, qt4)

#The ts() function converts a numeric vector into an R time series object 
ts1 <- ts(as.numeric((myDF[1,3:6])), frequency=4)
ts2 <- ts(as.numeric((myDF[2,3:6])), frequency=4)
ts3 <- ts(as.numeric((myDF[3,3:6])), frequency=4)
ts4 <- ts(as.numeric((myDF[4,3:6])), frequency=4)
ts5 <- ts(as.numeric((myDF[5,3:6])), frequency=4)

#Merge time series objects
tsm <- merge(a = as.zoo(ts1), b = as.zoo(ts2), c = as.zoo(ts3), d = as.zoo(ts4), e = as.zoo(ts5))

#Plotting the Time Series
plot.ts(tsm, plot.type = "single", lty = 1:5, xlab = "Time", ylab = "Sales")

コードは最適化されておらず、改善することができます。時系列分析の詳細については、こちらを参照してください。これが何らかの方向性を与えることを願っています。

ここに画像の説明を入力

于 2014-12-26T17:41:02.607 に答える