我正在尝试按时运行线性模型和每月因子,但是我收到了错误:
Error in model.frame.default(formula = ts.data ~ time2 + factor(month2), :
variable lengths differ (found for 'time2')
这是我创建变量的方式:
time2<-seq(along=ts.data)
month2<-rep(1:12,length=length(ts.data))
然而,运行长度(time2),长度(month2)和长度(ts.data)给出相同的数字,有谁知道我如何修复错误?
尝试运行lm:
lm(ts.data~time2+factor(month2))
我正在使用的数据:
structure(c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
64, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 1, 1, 2, 2, 2, 2, 2, 2,
2, 2, 2, 78238, 73928, 70708, 75175, 70744, 65604, 61227, 62635,
47652, 51507, 81874, 98236, 99401, 94756, 94697, 93732, 100334,
139355, 88575, 94169, 86084, 98249, 95321, 87822, 80256, 81875,
86293, 80712, 79533, 82847, 84498, 84185, 78382, 82701, 80491,
91140, 86847, 96727, 101295, 99450, 87783, 101246, 97913, 100081,
96346, 93608, 90648, 99105, 90920, 84960, 82591, 88090, 89980,
87778, 87429, 81898, 77285, 80369, 73193, 65139, 60126, 57219,
94204, 112472, 157199, 154791.5, 154294.88, 161920.63, 147408.75,
134418, 132158.5, 104572.5, 96831, 91045.88, 141182.63, 214759.25,
216647, 184598.38, 210794.38, 182403.75, 193001.63, 176807.38,
186552.63, 201375.88, 181861.25, 193234.88, 187240.25, 168242,
172475.13, 188996.25, 179663.88, 192861.63, 187461.25, 188670.5,
198826.25, 208696.5, 180490.75, 202265.88, 187966.13, 203342.13,
194850.38, 230582.63, 212517, 223432.5, 196511.63, 229582.25,
206120.63, 225629.88, 209769.63, 210797.63, 213215.75, 215144.88,
223266, 230747.63, 228573, 223828.88, 202102.88, 192863.63, 206675.13,
195647.5, 173897.25, 183788.88, 158511.38, 138559, 114163.25,
110399.13, 164751.13, 270772, 90430, 81719, 79183, 85428, 79372,
72361, 66207, 55403, 51693, 60280, 98698, 123059, 121550, 107662,
107863, 107630, 114685, 169659, 100104, 107598, 97728, 112850,
107784, 97580, 92709, 99098, 99482, 100543, 98856, 106081, 108248,
104769, 96966, 100093, 103107, 114944, 108001, 126289, 135213,
129717, 121688, 134421, 127318, 127412, 121922, 119045, 116989,
126286, 116707, 106627, 98219, 111225, 117279, 113725, 114633,
100633, 95478, 98394, 87616, 75329, 68274, 70658, 122995, 145224,
155833, 131896.5, 138340.63, 145610, 130653.13, 122562.75, 115850.5,
91749.88, 81787.13, 85457.5, 142931.63, 214970, 216836.63, 175902,
180757.88, 175233.63, 168982.13, 168727.25, 173501, 182731.38,
152260.63, 182607, 179326.5, 157693.13, 161004.75, 172990.5,
166204.38, 175172.63, 186446.13, 202645.38, 202500.25, 204148.38,
187763.5, 207269.75, 183334.88, 206552.5, 207270.13, 226123.88,
239037.88, 214656.38, 216552.75, 231406.75, 207365.63, 217873.5,
200308.88, 201696.5, 208984.75, 227723.38, 212083.25, 206262.38,
186596.25, 215496.63, 199399, 184933.25, 195925.63, 190318.63,
170375.38, 171624.13, 154537.13, 133532.25, 119179.13, 113297.88,
174946.5, 304690, 108567, 99358, 97299, 103628, 96936, 89254,
83761, 72058, 66685, 74491, 117292, 139878, 139585, 130180, 130079,
127562, 136152, 197149, 118619, 127875, 118094, 134989, 130688,
121475, 112367, 114805, 117087, 118526, 118038, 123988, 127511,
125790, 116702, 123049, 124260, 141232, 133809, 156349, 162637,
158367, 144491, 164389, 155305, 161401, 151829, 144188, 142702,
156405, 141937, 129857, 120318, 132823, 138201, 135058, 129275,
119897, 112924, 120385, 108134, 94062, 86695, 88434, 145426,
167100, 184196.63, 166628.38, 168193.13, 190280.88, 154984.75,
153784.38, 148033.75, 121304.25, 107303.25, 108003.13, 168770,
240983.75, 242817.13, 220229.38, 222805.75, 205068.75, 205204.25,
192598.25, 206565.38, 227284.88, 199258.25, 227122.88, 209076.13,
194855.5, 196357.25, 206865.88, 209580.13, 222190.88, 234610.88,
229339.13, 219321.63, 232571.75, 218584.75, 246116.38, 229563,
256776.75, 257335.25, 271507, 272014, 265850.5, 253426.63, 291759.63,
262608.88, 279417.25, 264583.25, 256634.88, 271024.88, 283927.13,
270597.38, 264222.5, 235009.13, 258379.25, 246485.5, 240163.25,
238369.88, 240961.5, 219826.75, 212077.5, 194937, 166299, 141284.88,
130153.38, 206775, 342062.88), .Dim = c(64L, 8L), .Dimnames = list(
NULL, c("Week_Number", "Campaign_Period", "Control_Traffic",
"Control_Revenue", "VOD_Test_Traffic", "VOD_Test_Revenue",
"TV_Test_Traffic", "TV_Test_revenue")), .Tsp = c(1, 2.21153846153846,
52), class = c("mts", "ts", "matrix"))
答案 0 :(得分:2)
如果我们根据行数创建分组变量,我们需要更改'month2'和'time2'。
month2<-rep(1:12,length=nrow(ts.data))
time2<-seq_len(nrow(ts.data))
res <- lm(ts.data~time2 + factor(month2))
coef(res)
# Week_Number Campaign_Period Control_Traffic Control_Revenue VOD_Test_Traffic VOD_Test_Revenue TV_Test_Traffic TV_Test_revenue
#(Intercept) 0.0000000000000213162821 3.384444444 78799.578 157220.4207 87712.9656 148735.2930 106055.5914 177326.337
#time2 0.9999999999999994448885 -0.017777778 123.605 727.3023 383.8344 966.6405 465.7336 1229.196
#factor(month2)2 0.0000000000000152835379 0.017777778 -1177.438 2858.4910 -1320.1678 -8588.9322 -1128.5669 -6726.196
#factor(month2)3 0.0000000000000008207055 0.035555556 5419.457 10544.3136 6779.6644 346.1573 7382.8661 1173.044
#factor(month2)4 0.0000000000000016587917 0.053333333 8603.018 27254.5313 12531.1633 23542.1418 12777.1325 28186.515
#factor(month2)5 0.0000000000000012801265 -0.268888889 3290.677 6985.0394 7531.8356 3638.7607 7201.7339 5969.374
#factor(month2)6 0.0000000000000020182926 -0.251111111 12858.272 5428.8610 20021.4011 4320.5682 21940.0003 7326.704
#factor(month2)7 0.0000000000000016906446 -0.233333333 1297.067 6299.8347 3690.1667 327.4537 2400.8667 -451.516
#factor(month2)8 0.0000000000000016516546 -0.015555556 1838.662 6690.3563 -832.6678 -2303.4348 445.1331 3647.310
#factor(month2)9 0.0000000000000015682557 0.002222222 -5728.743 -12651.4220 -7622.1022 -22135.3253 -8178.0006 -15978.562
#factor(month2)10 0.0000000000000003302248 0.020000000 -1715.348 -5722.0704 -2630.9367 -11870.0938 -2470.1342 -9128.055
#factor(month2)11 0.0000000000000022008184 0.037777778 1179.647 -5052.7747 1691.6289 -8744.7323 2258.9322 -5674.003
#factor(month2)12 0.0000000000000033608693 0.055555556 5039.042 4606.1469 5908.5944 4559.9012 7788.3986 6991.025