gpt4 book ai didi

r - tidy() 工作但 glance() 和 augment() 在回归模型中不起作用

转载 作者:行者123 更新时间:2023-12-01 12:03:13 27 4
gpt4 key购买 nike

我应用下面的代码,

代码:

rollingLinearModels <- rollify(
.f = function(price, date){
lm(price ~ date)
},
window = 30,
unlist = FALSE
)
out <- d %>%
mutate(
date = as.Date(date),
logPrice = log(Adjusted)
) %>%
group_by(.id) %>%
mutate(
models = rollingLinearModels(logPrice, date)
) %>%
mutate(tidymodels = map(models, ~tidy(.x)),
#glancemodels = map(models, ~glance(.x))
#augmentmodels = map(models, ~augment(.x))
)

效果很好,但是当我取消注释代码的 glance 和 augment 部分时,我遇到了错误。

Error: mutate() argument glancemodels errored. ℹ glancemodels is map(models, ~glance(.x)). ℹ The error occured in group 1: .id = "BLK". x No glance method for objects of class logical

为什么我可以应用 tidy() 而不能应用其他两种方法?我想使用模型中的 r.squared

数据:

d <- structure(list(.id = c("BLK", "BLK", "BLK", "BLK", "BLK", "BLK", 
"BLK", "BLK", "BLK", "BLK", "BLK", "BLK", "BLK", "BLK", "BLK",
"BLK", "BLK", "BLK", "BLK", "BLK", "BLK", "BLK", "BLK", "BLK",
"BLK", "BLK", "BLK", "BLK", "BLK", "BLK", "BLK", "BLK", "BLK",
"BLK", "BLK", "BLK", "BLK", "BLK", "BLK", "BLK", "BLK", "BLK",
"BLK", "BLK", "BLK", "BLK", "BLK", "BLK", "BLK", "BLK", "BLK",
"BLK", "BLK", "BLK", "BLK", "BLK", "BLK", "BLK", "BLK", "BLK",
"BLK", "BLK", "BLK", "BLK", "BLK", "BLK", "BLK", "BLK", "BLK",
"BLK", "BLK", "BLK", "BLK", "BLK", "BLK", "BLK", "BLK", "BLK",
"BLK", "BLK", "BLK", "BLK", "BLK", "BLK", "BLK", "BLK", "BLK",
"BLK", "BLK", "BLK", "BLK", "BLK", "BLK", "BLK", "BLK", "BLK",
"BLK", "BLK", "BLK", "BLK", "BLK", "BLK", "BLK", "BLK", "MSFT",
"MSFT", "MSFT", "MSFT", "MSFT", "MSFT", "MSFT", "MSFT", "MSFT",
"MSFT", "MSFT", "MSFT", "MSFT", "MSFT", "MSFT", "MSFT", "MSFT",
"MSFT", "MSFT", "MSFT", "MSFT", "MSFT", "MSFT", "MSFT", "MSFT",
"MSFT", "MSFT", "MSFT", "MSFT", "MSFT", "MSFT", "MSFT", "MSFT",
"MSFT", "MSFT", "MSFT", "MSFT", "MSFT", "MSFT", "MSFT", "MSFT",
"MSFT", "MSFT", "MSFT", "MSFT", "MSFT", "MSFT", "MSFT", "MSFT",
"MSFT", "MSFT", "MSFT", "MSFT", "MSFT", "MSFT", "MSFT", "MSFT",
"MSFT", "MSFT", "MSFT", "MSFT", "MSFT", "MSFT", "MSFT", "MSFT",
"MSFT", "MSFT", "MSFT", "MSFT", "MSFT", "MSFT", "MSFT", "MSFT",
"MSFT", "MSFT", "MSFT", "MSFT", "MSFT", "MSFT", "MSFT", "MSFT",
"MSFT", "MSFT", "MSFT", "MSFT", "MSFT", "MSFT", "MSFT", "MSFT",
"MSFT", "MSFT", "MSFT", "MSFT", "MSFT", "MSFT", "MSFT", "MSFT",
"MSFT", "MSFT", "MSFT", "MSFT", "MSFT", "MSFT", "MSFT"), date = c("2019-01-02",
"2019-01-03", "2019-01-04", "2019-01-07", "2019-01-08", "2019-01-09",
"2019-01-10", "2019-01-11", "2019-01-14", "2019-01-15", "2019-01-16",
"2019-01-17", "2019-01-18", "2019-01-22", "2019-01-23", "2019-01-24",
"2019-01-25", "2019-01-28", "2019-01-29", "2019-01-30", "2019-01-31",
"2019-02-01", "2019-02-04", "2019-02-05", "2019-02-06", "2019-02-07",
"2019-02-08", "2019-02-11", "2019-02-12", "2019-02-13", "2019-02-14",
"2019-02-15", "2019-02-19", "2019-02-20", "2019-02-21", "2019-02-22",
"2019-02-25", "2019-02-26", "2019-02-27", "2019-02-28", "2019-03-01",
"2019-03-04", "2019-03-05", "2019-03-06", "2019-03-07", "2019-03-08",
"2019-03-11", "2019-03-12", "2019-03-13", "2019-03-14", "2019-03-15",
"2019-03-18", "2019-03-19", "2019-03-20", "2019-03-21", "2019-03-22",
"2019-03-25", "2019-03-26", "2019-03-27", "2019-03-28", "2019-03-29",
"2019-04-01", "2019-04-02", "2019-04-03", "2019-04-04", "2019-04-05",
"2019-04-08", "2019-04-09", "2019-04-10", "2019-04-11", "2019-04-12",
"2019-04-15", "2019-04-16", "2019-04-17", "2019-04-18", "2019-04-22",
"2019-04-23", "2019-04-24", "2019-04-25", "2019-04-26", "2019-04-29",
"2019-04-30", "2019-05-01", "2019-05-02", "2019-05-03", "2019-05-06",
"2019-05-07", "2019-05-08", "2019-05-09", "2019-05-10", "2019-05-13",
"2019-05-14", "2019-05-15", "2019-05-16", "2019-05-17", "2019-05-20",
"2019-05-21", "2019-05-22", "2019-05-23", "2019-05-24", "2019-05-28",
"2019-05-29", "2019-05-30", "2019-05-31", "2019-01-02", "2019-01-03",
"2019-01-04", "2019-01-07", "2019-01-08", "2019-01-09", "2019-01-10",
"2019-01-11", "2019-01-14", "2019-01-15", "2019-01-16", "2019-01-17",
"2019-01-18", "2019-01-22", "2019-01-23", "2019-01-24", "2019-01-25",
"2019-01-28", "2019-01-29", "2019-01-30", "2019-01-31", "2019-02-01",
"2019-02-04", "2019-02-05", "2019-02-06", "2019-02-07", "2019-02-08",
"2019-02-11", "2019-02-12", "2019-02-13", "2019-02-14", "2019-02-15",
"2019-02-19", "2019-02-20", "2019-02-21", "2019-02-22", "2019-02-25",
"2019-02-26", "2019-02-27", "2019-02-28", "2019-03-01", "2019-03-04",
"2019-03-05", "2019-03-06", "2019-03-07", "2019-03-08", "2019-03-11",
"2019-03-12", "2019-03-13", "2019-03-14", "2019-03-15", "2019-03-18",
"2019-03-19", "2019-03-20", "2019-03-21", "2019-03-22", "2019-03-25",
"2019-03-26", "2019-03-27", "2019-03-28", "2019-03-29", "2019-04-01",
"2019-04-02", "2019-04-03", "2019-04-04", "2019-04-05", "2019-04-08",
"2019-04-09", "2019-04-10", "2019-04-11", "2019-04-12", "2019-04-15",
"2019-04-16", "2019-04-17", "2019-04-18", "2019-04-22", "2019-04-23",
"2019-04-24", "2019-04-25", "2019-04-26", "2019-04-29", "2019-04-30",
"2019-05-01", "2019-05-02", "2019-05-03", "2019-05-06", "2019-05-07",
"2019-05-08", "2019-05-09", "2019-05-10", "2019-05-13", "2019-05-14",
"2019-05-15", "2019-05-16", "2019-05-17", "2019-05-20", "2019-05-21",
"2019-05-22", "2019-05-23", "2019-05-24", "2019-05-28", "2019-05-29",
"2019-05-30", "2019-05-31"), Open = c(386.279999, 385.029999,
385.170013, 390.940002, 397.76001, 399.5, 399.119995, 395.01001,
392.869995, 396.549988, 402.089996, 411, 417.079987, 416.410004,
413.029999, 407.910004, 411.220001, 405.850006, 411.5, 409.200012,
406.220001, 417.440002, 416.5, 418.119995, 418.440002, 414.350006,
412.149994, 413.75, 416.190002, 426.700012, 424.410004, 425.190002,
428.429993, 431.779999, 436.600006, 435.519989, 439.279999, 437.670013,
440.170013, 442.01001, 447.559998, 445.890015, 434.25, 432.519989,
434.440002, 420.570007, 424.01001, 429.630005, 430.329987, 435.459991,
433.829987, 436, 442.549988, 434.200012, 428.589996, 426.51001,
418, 420.570007, 423.600006, 421.399994, 429.709991, 431.589996,
438.390015, 439.220001, 439.170013, 443.170013, 444.540009, 445.410004,
441.600006, 445, 448.690002, 456.359985, 453.100006, 466.980011,
468.779999, 463.75, 464, 474.029999, 477.890015, 475.109985,
480, 479.73999, 486.839996, 479.51001, 476.51001, 472.920013,
473.609985, 462.179993, 458.730011, 461.850006, 453.119995, 444.359985,
441.48999, 446.51001, 441.829987, 439.600006, 440.820007, 440.160004,
434.859985, 437, 436.170013, 426.390015, 428.480011, 420.75,
99.550003, 100.099998, 99.720001, 101.639999, 103.040001, 103.860001,
103.220001, 103.190002, 101.900002, 102.510002, 105.260002, 105,
107.459999, 106.75, 106.120003, 106.860001, 107.239998, 106.260002,
104.879997, 104.620003, 103.800003, 103.779999, 102.870003, 106.059998,
107, 105.190002, 104.389999, 106.199997, 106.139999, 107.5, 106.309998,
107.910004, 107.790001, 107.860001, 106.900002, 110.050003, 111.760002,
111.260002, 111.690002, 112.040001, 112.889999, 113.019997, 112.25,
111.870003, 111.400002, 109.160004, 110.989998, 112.82, 114.129997,
114.540001, 115.339996, 116.169998, 118.089996, 117.389999, 117.139999,
119.5, 116.559998, 118.620003, 117.879997, 117.440002, 118.07,
118.949997, 119.059998, 119.860001, 120.099998, 119.389999, 119.809998,
118.629997, 119.760002, 120.540001, 120.639999, 120.940002, 121.639999,
121.239998, 122.190002, 122.620003, 124.099998, 125.790001, 130.059998,
129.699997, 129.899994, 129.809998, 130.529999, 127.980003, 127.360001,
126.389999, 126.459999, 125.440002, 124.290001, 124.910004, 124.110001,
123.870003, 124.260002, 126.75, 128.309998, 126.519997, 127.43,
126.620003, 126.199997, 126.910004, 126.980003, 125.379997, 125.260002,
124.230003), High = c(391.160004, 386.549988, 392.929993, 398.179993,
400.850006, 403.149994, 400.920013, 399.630005, 399.950012, 401.119995,
421.829987, 416.940002, 420.559998, 419.200012, 414.570007, 411.329987,
414.070007, 407.880005, 412, 410.940002, 415.959991, 417.579987,
418.73999, 419.140015, 420.779999, 417.880005, 414.049988, 414.869995,
425.75, 433.75, 425.269989, 432.630005, 433, 437.48999, 437.589996,
436.950012, 444.799988, 443.01001, 444.700012, 444.720001, 451.920013,
447, 435.709991, 434, 434.980011, 421.890015, 429.390015, 431.019989,
433.399994, 435.459991, 438.820007, 440.75, 443.359985, 434.700012,
433.700012, 428.429993, 419.5, 423.309998, 425.529999, 425.640015,
432, 439.640015, 440.100006, 442.799988, 442.700012, 446.829987,
447.679993, 445.940002, 442.940002, 448.75, 457.329987, 457.100006,
466.890015, 468.779999, 469.369995, 465.660004, 474.029999, 482.369995,
480.480011, 479.459991, 481.579987, 485.850006, 487.450012, 480.170013,
482.899994, 478.619995, 473.929993, 469.459991, 464.459991, 465.640015,
454.390015, 451.059998, 446.390015, 450.679993, 447.359985, 440.279999,
444.320007, 442.079987, 434.859985, 438.230011, 436.700012, 428.450012,
431.480011, 420.75, 101.75, 100.190002, 102.510002, 103.269997,
103.970001, 104.879997, 103.75, 103.440002, 102.870003, 105.050003,
106.260002, 106.629997, 107.900002, 107.099998, 107.040001, 107,
107.879997, 106.480003, 104.970001, 106.379997, 105.220001, 104.099998,
105.800003, 107.269997, 107, 105.589996, 105.779999, 106.580002,
107.139999, 107.779999, 107.290001, 108.300003, 108.660004, 107.940002,
109.480003, 111.199997, 112.18, 113.239998, 112.360001, 112.879997,
113.019997, 113.25, 112.389999, 112.660004, 111.550003, 110.709999,
112.949997, 113.989998, 115, 115.199997, 117.25, 117.610001,
118.440002, 118.75, 120.82, 119.589996, 118.010002, 118.709999,
118.209999, 117.580002, 118.32, 119.110001, 119.480003, 120.43,
120.230003, 120.230003, 120.019997, 119.540001, 120.349998, 120.849998,
120.980003, 121.580002, 121.650002, 121.849998, 123.519997, 124,
125.580002, 125.849998, 131.369995, 130.520004, 130.179993, 130.699997,
130.649994, 128, 129.429993, 128.559998, 127.18, 126.370003,
125.790001, 127.93, 125.550003, 125.879997, 126.709999, 129.380005,
130.460007, 127.589996, 127.529999, 128.240005, 126.290001, 127.419998,
128, 125.389999, 125.760002, 124.620003), Low = c(385.100006,
377.279999, 381.98999, 389.420013, 393.600006, 395.299988, 395.220001,
389.670013, 391.109985, 393.399994, 401.429993, 403.690002, 413.589996,
411.089996, 403.76001, 401.799988, 409.149994, 402.399994, 406.290009,
401.820007, 406.220001, 412.119995, 413.170013, 414.049988, 415.070007,
410.01001, 406.130005, 411, 415.429993, 426.100006, 417.950012,
425.190002, 428.170013, 429.769989, 432.850006, 433.450012, 438.040009,
435.690002, 438.880005, 440.390015, 442.100006, 434.059998, 431.149994,
429.390015, 420.540009, 417.209991, 422.920013, 426.880005, 426.5,
431.109985, 432.929993, 435.940002, 433.649994, 427.559998, 424.200012,
415.130005, 413.299988, 418.940002, 417.709991, 420.230011, 426.190002,
429.399994, 435.470001, 437.309998, 438.920013, 440.609985, 443.100006,
438.519989, 439.170013, 442.869995, 448.690002, 449.769989, 453,
462.420013, 465.690002, 462.149994, 463.899994, 472.609985, 472.609985,
471.850006, 478.609985, 477.570007, 478.709991, 471.470001, 476.410004,
469.869995, 459.059998, 459.619995, 454.160004, 453.25, 440.190002,
444.359985, 439.429993, 445.769989, 440.23999, 434.480011, 440.190002,
436.380005, 429.100006, 434.799988, 429.309998, 422.630005, 423.809998,
415.019989, 98.940002, 97.199997, 98.93, 100.980003, 101.709999,
103.239998, 102.379997, 101.639999, 101.260002, 101.879997, 104.959999,
104.760002, 105.910004, 104.860001, 105.339996, 105.339996, 106.199997,
104.660004, 102.169998, 104.330002, 103.18, 102.349998, 102.769997,
105.959999, 105.529999, 104.290001, 104.260002, 104.970001, 105.480003,
106.709999, 105.660004, 107.360001, 107.779999, 106.290001, 106.870003,
109.82, 111.260002, 111.169998, 110.879997, 111.730003, 111.669998,
110.800003, 111.230003, 111.43, 109.870003, 108.800003, 110.980003,
112.650002, 113.779999, 114.330002, 114.589996, 116.050003, 116.989998,
116.709999, 117.089996, 117.040001, 116.32, 116.849998, 115.519997,
116.129997, 116.959999, 118.099998, 118.519997, 119.150002, 118.379997,
119.370003, 118.639999, 118.580002, 119.540001, 119.919998, 120.370003,
120.57, 120.099998, 120.540001, 121.300003, 122.57, 123.830002,
124.519997, 128.830002, 129.020004, 129.350006, 129.389999, 127.699997,
125.519997, 127.25, 126.110001, 124.220001, 124.75, 123.57, 123.82,
123.040001, 123.699997, 123.699997, 126.459999, 127.919998, 125.760002,
126.580002, 126.519997, 124.739998, 125.970001, 126.050003, 124.040001,
124.779999, 123.32), Close = c(389.420013, 377.980011, 391.820007,
392.910004, 397.910004, 400.190002, 399.5, 397.910004, 396.420013,
400.709991, 413.040009, 412.519989, 419.450012, 413.600006, 408.480011,
406.559998, 409.950012, 405.359985, 407.619995, 408.329987, 415.079987,
416.809998, 417.980011, 417.559998, 416.390015, 415.01001, 411.76001,
412.700012, 425.51001, 427.450012, 423.380005, 431.950012, 431.070007,
437.220001, 435.019989, 436.720001, 438.929993, 440.529999, 442.01001,
443.220001, 443.769989, 437.329987, 432.519989, 429.859985, 423.190002,
421.309998, 428.100006, 427.839996, 431.839996, 433.630005, 433.549988,
440.480011, 434.570007, 429.920013, 430.359985, 417.359985, 415.410004,
422.670013, 419.640015, 424.970001, 427.369995, 438.390015, 436.450012,
439.070007, 441.839996, 445.100006, 446.140015, 439.809998, 442.76001,
446.109985, 454.350006, 451.859985, 466.540009, 467.48999, 465.690002,
464.019989, 474.029999, 480.170013, 474.450012, 478.980011, 479.839996,
485.23999, 479.130005, 476.410004, 482.5, 476.369995, 463.769989,
465.470001, 464.220001, 463, 442.779999, 445.859985, 444.359985,
447.23999, 441.559998, 438.089996, 441.660004, 439.140015, 433.890015,
435.76001, 429.309998, 427.929993, 426.589996, 415.559998, 101.120003,
97.400002, 101.93, 102.059998, 102.800003, 104.269997, 103.599998,
102.800003, 102.050003, 105.010002, 105.379997, 106.120003, 107.709999,
105.68, 106.709999, 106.199997, 107.169998, 105.080002, 102.940002,
106.379997, 104.43, 102.779999, 105.739998, 107.220001, 106.029999,
105.269997, 105.669998, 105.25, 106.889999, 106.809998, 106.900002,
108.220001, 108.169998, 107.150002, 109.410004, 110.970001, 111.589996,
112.360001, 112.169998, 112.029999, 112.529999, 112.260002, 111.699997,
111.75, 110.389999, 110.510002, 112.830002, 113.620003, 114.5,
114.589996, 115.910004, 117.57, 117.650002, 117.519997, 120.220001,
117.050003, 117.660004, 117.910004, 116.769997, 116.93, 117.940002,
119.019997, 119.190002, 119.970001, 119.360001, 119.889999, 119.93,
119.279999, 120.190002, 120.330002, 120.949997, 121.050003, 120.769997,
121.769997, 123.370003, 123.760002, 125.440002, 125.010002, 129.149994,
129.889999, 129.770004, 130.600006, 127.879997, 126.209999, 128.899994,
128.149994, 125.519997, 125.510002, 125.5, 127.129997, 123.349998,
124.730003, 126.019997, 128.929993, 128.070007, 126.220001, 126.900002,
127.669998, 126.18, 126.239998, 126.160004, 124.940002, 125.730003,
123.68), Volume = c(926800, 781000, 647800, 714500, 642900, 640500,
487100, 706500, 919700, 846600, 1331500, 917200, 1031800, 905600,
660100, 838400, 573000, 640400, 602800, 869900, 750100, 585900,
534800, 523400, 424600, 469400, 567000, 595600, 617300, 803500,
542500, 600300, 438000, 563300, 451800, 400300, 516300, 472000,
461100, 512100, 657200, 686100, 433500, 272700, 467400, 587100,
593100, 504700, 606700, 501700, 1756900, 550700, 948500, 881600,
472400, 585300, 411000, 494900, 425000, 342700, 455900, 498900,
423100, 337800, 375200, 458400, 398400, 460900, 402900, 412700,
628700, 590800, 1030700, 628200, 530400, 432900, 687400, 1008500,
598700, 469000, 366900, 567600, 488600, 436100, 403300, 434400,
709500, 454800, 435500, 484000, 703900, 426800, 424200, 449100,
496900, 465700, 307000, 331000, 577600, 277300, 462600, 390700,
412400, 534600, 35329300, 42579100, 44060600, 35656100, 31514400,
32280800, 30067600, 28314200, 28437100, 31587600, 29853900, 28393000,
37427600, 32371300, 25874300, 23164800, 31225600, 29476700, 31490500,
49471900, 55636400, 35535700, 31315100, 27325400, 20609800, 29760700,
21461100, 18914100, 25056600, 18394900, 21784700, 26606900, 18038500,
21607700, 29063200, 27763200, 23750600, 21536700, 21487100, 29083900,
23501200, 26608000, 19538300, 17687000, 25339000, 22818400, 26491600,
26132700, 35513800, 30763400, 54681100, 31207600, 37588700, 28113300,
29854400, 33624500, 27067100, 26097700, 22733400, 18334800, 25399800,
22789100, 18142300, 22860700, 20112800, 15826200, 15116200, 17612000,
16477200, 14209100, 19745100, 15792600, 14071800, 19300900, 27991000,
15648700, 24025500, 31257000, 38033900, 23654900, 16324200, 24166500,
26821700, 27350200, 24911100, 24239800, 36017700, 28419000, 27235800,
30915100, 33944900, 25266300, 24722700, 30112200, 25770500, 23706900,
15293300, 15396500, 23603800, 14123400, 23128400, 22763100, 16829600,
26646800), Adjusted = c(377.922577, 366.820343, 380.25174, 381.309509,
386.161896, 388.374573, 387.704956, 386.161896, 384.715912, 388.879242,
400.845215, 400.340515, 407.065948, 401.388672, 396.41983, 394.556519,
397.846466, 393.391907, 395.585205, 396.274231, 402.824951, 404.503876,
405.639313, 405.23175, 404.096313, 402.75705, 399.602966, 400.515259,
412.947021, 414.829742, 410.879883, 419.196869, 418.342896, 424.311279,
422.176208, 423.82608, 425.970764, 427.52356, 428.9599, 430.134155,
430.667877, 424.41803, 422.941467, 420.340393, 413.818085, 411.979736,
418.619354, 418.365112, 422.27655, 424.026886, 423.948669, 430.72522,
424.946075, 420.399078, 420.829285, 408.117188, 406.210388, 413.309601,
410.34671, 415.558655, 417.905518, 428.681488, 426.784454, 429.346436,
432.055054, 435.242859, 436.259857, 430.070007, 432.954712, 436.230499,
444.288055, 441.853119, 456.208069, 457.137024, 455.376892, 453.743835,
463.532196, 469.536224, 463.942902, 468.372589, 469.213531, 474.493958,
468.519257, 465.859497, 471.814606, 465.820374, 453.49939, 455.161743,
453.939453, 452.746429, 432.974243, 435.986053, 434.519257, 437.33548,
431.78125, 428.388092, 431.879059, 429.414886, 424.281158, 426.109711,
419.802582, 418.453094, 417.142792, 406.357056, 99.375191, 95.719376,
100.171211, 100.298965, 101.026199, 102.470825, 101.812401, 101.026199,
100.289146, 103.198067, 103.561684, 104.288918, 105.851479, 103.856506,
104.868729, 104.367531, 105.320793, 103.266861, 101.163788, 104.544426,
102.628075, 101.006546, 103.915466, 105.369934, 104.20047, 103.453575,
103.84668, 103.433922, 105.045631, 104.967003, 105.05545, 106.352676,
106.303543, 105.750854, 107.981354, 109.520973, 110.132874, 110.892822,
110.705299, 110.567131, 111.060608, 110.794128, 110.241432, 110.290787,
108.94854, 109.066978, 111.356689, 112.136368, 113.004875, 113.093697,
114.396469, 116.034782, 116.113747, 115.985443, 118.650192, 115.521576,
116.123627, 116.370346, 115.245224, 115.403145, 116.399963, 117.465858,
117.633636, 118.403458, 117.801422, 118.324493, 118.363968, 117.72245,
118.620583, 118.758751, 119.370651, 119.469353, 119.193001, 120.179947,
121.759056, 122.143959, 123.802032, 123.377647, 127.463577, 128.193909,
128.075485, 128.894653, 126.210159, 124.561966, 127.216843, 126.476624,
123.880966, 123.871117, 123.861244, 125.469955, 121.739311, 123.101303,
124.834839, 127.717468, 126.86557, 125.032967, 125.706573, 126.469322,
124.99334, 125.052765, 124.973534, 123.764999, 124.547562, 122.516846
)), class = "data.frame", row.names = c(NA, -208L))

最佳答案

这个问题是基于一些list元素中的NA_logical_,我们可以使用if/else条件

library(dplyr) #devel version
library(broom)
library(tibbletime)
library(purrr)
d %>%
mutate(date = as.Date(date), logPrice = log(Adjusted)) %>%
group_by(.id) %>%
condense(models = rollingLinearModels(logPrice, date),
tidymodels = map(models, tidy),
glancemodels = map(models, ~ if(!is.na(.x)) glance(.x) else NA),
augmentmodels = map(models, ~ if(!is.na(.x)) augment(.x) else NA))
# A tibble: 2 x 5
# Rowwise: .id
# .id models tidymodels glancemodels augmentmodels
# <chr> <list> <list> <list> <list>
#1 BLK <list [104]> <list [104]> <list [104]> <list [104]>
#2 MSFT <list [104]> <list [104]> <list [104]> <list [104]>

如果我们对单个元素执行此操作,它将能够复制

 d %>% 
mutate(date = as.Date(date), logPrice = log(Adjusted)) %>%
group_by(.id) %>%
condense(models = rollingLinearModels(logPrice, date)) %>%
.$models %>%
magrittr::extract2(1) %>%
magrittr::extract2(104) %>%
glance
# A tibble: 1 x 11
# r.squared adj.r.squared sigma statistic p.value df logLik AIC BIC deviance df.residual
# <dbl> <dbl> <dbl> <dbl> <dbl> <int> <dbl> <dbl> <dbl> <dbl> <int>
#1 0.820 0.813 0.0195 127. 6.31e-12 2 76.6 -147. -143. 0.0106 28

现在,在 NA

的元素上尝试
d %>% 
mutate(date = as.Date(date), logPrice = log(Adjusted)) %>%
group_by(.id) %>%
condense(models = rollingLinearModels(logPrice, date)) %>%
.$models %>%
magrittr::extract2(1) %>%
magrittr::extract2(1)
#[1] NA
d %>%
mutate(date = as.Date(date), logPrice = log(Adjusted)) %>%
group_by(.id) %>%
condense(models = rollingLinearModels(logPrice, date)) %>%
.$models %>%
magrittr::extract2(1) %>%
magrittr::extract2(1) %>%
glance

Error: No glance method for objects of class logical

tidy 之所以有效,是因为它创建了一个 data.frame,其中包含具有“NA”值的单个列“x”

d %>% 
mutate(date = as.Date(date), logPrice = log(Adjusted)) %>%
group_by(.id) %>%
condense(models = rollingLinearModels(logPrice, date)) %>%
.$models %>%
magrittr::extract2(1) %>%
magrittr::extract2(1) %>%
tidy
# A tibble: 1 x 1
# x
# <lgl>
#1 NA

此外,通过检查每个函数的方法的数量

length(methods("tidy"))
#[1] 115
length(methods("glance"))
#[1] 64
length(methods('augment'))
#[1] 36

所以,'tidy' 可能会处理很多情况,尤其是有一种方法 tidy.NULL*

关于r - tidy() 工作但 glance() 和 augment() 在回归模型中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60441431/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com