繁体   English   中英

如何在 logit 回归中使用预测组

[英]How to use predict with groups in logit regression

我正在为每只股票(=LPERMNO)在 R 中运行逻辑回归 model,这就是为什么我在运行 glm 之前对数据集进行分组的原因。 但是,当我尝试进行预测时出现以下错误:

没有适用于“预测”的方法应用于 class 的 object "c('rowwise_df', 'tbl_df', 'tbl', 'data.frame')

我的目标是收到一张表,其中包含每只股票(LPERMNO)的 JACKPOT(=variable =1,而不是 Jackpot=0)的预测概率。

尝试复制此处使用的方法,我仍然收到预测 function 的相同错误消息。

到目前为止,我的代码看起来像这样

# model data 
model_input
# fit data to run the regression on 
fit_data
# run logit model on model input 
> model <-model_input %>%
+   group_by(LPERMNO)%>%
+   do( model = glm(JACKPOT 
+                   ~ AGE+TANG+SALESGRTH+TURN+SIZE+SDEV+SKEW+RET12,
+                   data=., 
+                   family=binomial))
Warnmeldungen:
1: glm.fit: algorithm did not converge 
2: glm.fit: fitted probabilities numerically 0 or 1 occurred 
3: glm.fit: algorithm did not converge 
4: glm.fit: fitted probabilities numerically 0 or 1 occurred 
> # view model
> library(broom)
> model %>% tidy(model)
# A tibble: 16 x 6
# Groups:   LPERMNO [2]
   LPERMNO term            estimate   std.error  statistic p.value
     <int> <chr>              <dbl>       <dbl>      <dbl>   <dbl>
 1   10011 (Intercept)  -759.        3350570.   -0.000227    1.000
 2   10011 TANG         2673.       10422493.    0.000256    1.000
 3   10011 SALESGRTH     421.        5847097.    0.0000720   1.000
 4   10011 TURN           -0.000556        3.27 -0.000170    1.000
 5   10011 SIZE         -121.        1401422.   -0.0000860   1.000
 6   10011 SDEV        -2718.        6750572.   -0.000403    1.000
 7   10011 SKEW           20.7        141960.    0.000146    1.000
 8   10011 RET12        -106.         395987.   -0.000267    1.000
 9   10032 (Intercept)  2509.        2388459.    0.00105     0.999
10   10032 TANG        -5578.        3747618.   -0.00149     0.999
11   10032 SALESGRTH     324.         249059.    0.00130     0.999
12   10032 TURN           -0.00256         6.43 -0.000398    1.000
13   10032 SIZE         -144.         218359.   -0.000661    0.999
14   10032 SDEV         5982.        3605058.    0.00166     0.999
15   10032 SKEW          244.         127777.    0.00191     0.998
16   10032 RET12        -142.          81402.   -0.00174     0.999
> 
> # fit model to new data 
> fitted.results <- predict(model,newdata= fit_data,type='response')
Fehler in UseMethod("predict") : 
  nicht anwendbare Methode für 'predict' auf Objekt der Klasse "c('rowwise_df', 'tbl_df', 'tbl', 'data.frame')" angewendet
> 
> # define decision boundary
> fitted.results <- ifelse(fitted.results > 0.5,1,0)

请在下面找到用于 model_data 和 fit_data 的 dput output(对不起,对于大型数据集,但我发现当数据集太小时时 glm 不起作用)

模型数据:

结构(列表(lpermno = C(10011L,10011L,10011L,10011L,10011L,10011L,10011L,10011L,10011L,10011L,10011L,10011L,10011L,10011L,10011L,10011L,10011L,10011L,10011L,10011L,10011L,10011L,10011L,10011L,10011L,10011L,10011L,10011L,10011L,10011L,10011L,10011L,10011L,10011L,,10011L,,10011L,,10011L,,,,地10011L, 10011L, 10011L, 10011L, 10011L, 10011L, 10011L, 10011L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L), year_mon = structure(c(7517, 7548, 7578, 7609, 7639, 7670 ,7701、7729、7760、7790、7821、7851、7882、7913、7943、7943、7974、8004、8035、8066、8095、8126、8126、8156、8156、8187 , 6695, 6726, 6756, 6787, 6818, 6848, 6879, 6909, 6940, 6971, 6999, 7 030, 7060, 7091, 7121, 7152, 7183, 7213, 7244, 7274, 7305, 7336, 7364, 7395, 7425, 7456, 7486, 7517, 7548, 7578, 7609, 7639, 7670, 7701, 7729, 7760, 7790, 7821, 7851, 7882, 7913, 7943, 7974, 8004, 8035, 8066, 8095, 8126, 8156, 8187, 8217, 8248, 8279, 8309, 8340, 8370), class = c("yearmonth", "日期")), fyear = c(1989L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1990L, NA, NA, NA, NA, NA, NA, NA, NA , NA, NA, NA, 1991L, NA, NA, NA, NA, 1987L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1988L, NA, NA, NA, NA不适用,NA,NA,NA,NA,NA,NA,1991L,NA,NA,NA,NA,NA,NA,NA,NA,NA),年龄= c(31L,31L,31L,31L,31L,31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 3 3L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L), TANG = c(0.415129151291513, 0.415129151291513, 0.415129151291513, 0.415129151291513, 0.415129151291513, 0.415129151291513, 0.415129151291513, 0.415129151291513, 0.415129151291513, 0.415129151291513, 0.415129151291513, 0.415129151291513, 0.384976525821596, 0.384976525821596, 0.384976525821596, 0.384976525821596 , 0.384976525821596, 0.384976525821596, 0.384976525821596, 0.384976525821596, 0.384976525821596, 0.384976525821596, 0.384976525821596, 0.384976525821596, 0.445286797310828, 0.445286797310828, 0.445286797310828, 0.445286797310828, 0.445286797310828, 0.513819779900483, 0.513819779900483, 0.513819779900483, 0.513819779900483, 0.513819779900483, 0.513819779900483, 0.513819779900483, 0.513819779900483, 0.513819779900483, 0.513819779900483, 0.513819779900483, 0.513819779900483 , 0.471875, 0.471875, 0.471875, 0.471875, 0.471875, 0。 471875, 0.471875, 0.471875, 0.471875, 0.471875, 0.471875, 0.471875, 0.457163229486617, 0.457163229486617, 0.457163229486617, 0.457163229486617, 0.457163229486617, 0.457163229486617, 0.457163229486617, 0.457163229486617, 0.457163229486617, 0.457163229486617, 0.457163229486617, 0.457163229486617, 0.438609825191736, 0.438609825191736, 0.438609825191736, 0.438609825191736, 0.438609825191736, 0.438609825191736, 0.438609825191736, 0.438609825191736, 0.438609825191736, 0.438609825191736, 0.438609825191736, 0.438609825191736, 0.404720423994572, 0.404720423994572, 0.404720423994572, 0.404720423994572, 0.404720423994572, 0.404720423994572, 0.404720423994572, 0.404720423994572, 0.404720423994572, 0.404720423994572), SALESGRTH = c(0.276258287990349, 0.276258287990349, 0.276258287990349, 0.276258287990349, 0.276258287990349, 0.276258287990349, 0.276258287990349 , 0.276258287990349, 0.276258287990349, 0.276258287990349, 0.276258287990349, 0.276258287990349, 0.120193238812454, 0.120,10.324882424 193238812454, 0.120193238812454, 0.120193238812454, 0.120193238812454, 0.120193238812454, 0.120193238812454, 0.120193238812454, 0.120193238812454, 0.120193238812454, 0.120193238812454, 0.0134191878058735, 0.0134191878058735, 0.0134191878058735, 0.0134191878058735, 0.0134191878058735, 0.02448259345339, 0.02448259345339, 0.02448259345339, 0.02448259345339, 0.02448259345339, 0.02448259345339, 0.02448259345339, 0.02448259345339, 0.02448259345339, 0.02448259345339, 0.02448259345339, 0.02448259345339, 0.773688592084308, 0.773688592084308, 0.773688592084308, 0.773688592084308, 0.773688592084308, 0.773688592084308, 0.773688592084308, 0.773688592084308, 0.773688592084308, 0.773688592084308, 0.773688592084308, 0.773688592084308, 0.384379273702973, 0.384379273702973, 0.384379273702973, 0.384379273702973, 0.384379273702973, 0.384379273702973, 0.384379273702973, 0.384379273702973, 0.384379273702973, 0.384379273702973, 0.384379273702973, 0.384379273702973, 0.00807613522107376, 0.00807613522107376, 0 .00807613522107376, 0.00807613522107376, 0.00807613522107376, 0.00807613522107376, 0.00807613522107376, 0.00807613522107376, 0.00807613522107376, 0.00807613522107376, 0.00807613522107376, 0.00807613522107376, 0.425509556825718, 0.425509556825718, 0.425509556825718, 0.425509556825718, 0.425509556825718, 0.425509556825718, 0.425509556825718, 0.425509556825718, 0.425509556825718, 0.425509556825718), date_endm.x = structure(c( 7547, 7577, 7608, 7638, 7669, 7700, 7728, 7759, 7789, 7820, 7850, 7881, 7912, 7942, 7973, 8003, 8034, 8065, 8094, 8125, 8155, 8186, 8216, 8247, 8278, 8308,8339,8369,8400,6664,6694,6694,6725,6755,6786,6817,6817,6847,6878,6908,6939,6970 7304, 7335, 7363, 7394, 7424, 7455, 7485, 7516, 7547, 7577, 7608, 7638, 7669, 7700, 7728, 7759, 7789, 7820, 7850, 7881, 7912, 7942, 7973, 8003, 8034, 8065, 8094, 8125, 8155, 8186, 8216, 8247, 8278, 8308, 8339, 8369, 8400), class = "日期"), 转 = c(17865.973382204, 17865.973382204, 17865.973382204, 7392.91945592125, 7392.91945592125, 7392.91945592125, 19865.8884651975, 19865.8884651975, 19865.8884651975, 24565.9432428495, 24565.9432428495, 24565.9432428495, 1419.90252973571, 1419.90252973571, 1419.90252973571, -2213.87866817942, -2213.87866817942, -2213.87866817942, -15403.1013380001, -15403.1013380001, -15403.1013380001 , -1785.84264896681, -1785.84264896681, -1785.84264896681, -4991.8142242685, -4991.8142242685, -4991.8142242685, -184218.671539397, -184218.671539397, -6234.32230817762, -6234.32230817762, -6234.32230817762, 8924.76931918873, 8924.76931918873, 8924.76931918873, 12331.8255104198, 12331.8255104198, 12331.8255104198, 8811.45418860484, 8811.45418860484, 8811.45418860484, 803.413220624327, 803.413220624327, 803.413220624327, -8444.78974302436, -8444.78974302436, -8444.78974302436, -26838.2771363143, -26838.2771363143, -26838.2771363143, -27867.0438048438, -27867.0438048438, -27867.0438048438, 3973.82622435177, 3973 .82622435177, 3973.82622435177, 7603.46869250673, 7603.46869250673, 7603.46869250673, 9231.6927696502, 9231.6927696502, 9231.6927696502, 15828.7611638797, 15828.7611638797, 15828.7611638797, 5224.77196272685, 5224.77196272685, 5224.77196272685, 7128.27348227299, 7128.27348227299, 7128.27348227299, -25137.1781282815, -25137.1781282815, -25137.1781282815, -117894.642421525, -117894.642421525, -117894.642421525, -114951.933700383, -114951.933700383, -114951.933700383, -21106.4960063974, -21106.4960063974, -21106.4960063974, 17465.7301902583, 17465.7301902583, 17465.7301902583, -18408.3389599337), SIZE = c(3.24483592653773, 3.24483592653773, 3.24483592653773, 3.01599435410888, 3.01599435410888, 3.01599435410888, 3.31378592465119, 3.31378592465119 , 3.31378592465119, 3.26726590901629, 3.26726590901629, 3.26726590901629, 2.9050619380051, 2.9050619380051, 2.9050619380051, 2.83360297402295, 2.83360297402295, 2.83360297402295, 2.83360297402295, 2.83360297402295, 2.83360297402295, 2.5826759247500 1, 2.58267592475001, 2.58267592475001, 2.63146608891944, 2.63146608891944, 2.63146608891944, 3.14229171268543, 3.14229171268543, 2.85497483583773, 2.85497483583773, 2.85497483583773, 2.82512187268805, 2.82512187268805, 2.82512187268805, 2.97351222665215, 2.97351222665215, 2.97351222665215, 2.91944500538187, 2.91944500538187, 2.91944500538187, 3.32491011349004, 3.32491011349004, 3.32491011349004, 3.57003257152302, 3.57003257152302, 3.57003257152302, 3.64023396697828, 3.64023396697828, 3.64023396697828, 3.32757775178839, 3.32757775178839, 3.32757775178839, 3.27041933794844, 3.27041933794844, 3.27041933794844, 3.43293826744622, 3.43293826744622, 3.43293826744622, 3.14670827656614, 3.14670827656614, 3.14670827656614, 2.92356472525193, 2.92356472525193, 2.92356472525193, 3.29128950537725, 3.29128950537725, 3.29128950537725, 3.40097235634586, 3.40097235634586, 3.40097235634586, 3.79608451754706, 3.79608451754706, 3.79608451754706, 4.38876637153694, 4.38876637153694, 4.38876637153694, 4.72091 78182824, 4.7209178182824, 4.7209178182824, 4.82279855661419, 4.82279855661419, 4.82279855661419, 4.90829244292775, 4.90829244292775, 4.90829244292775, 4.57182020630654), SDEV = c(0.0131660867611169, 0.0227885077248578, 0.0385063774922944, 0.0453701832587235, 0.0517023216849306, 0.0540557778054221, 0.0488190684785587, 0.0421655604203246, 0.0325068730742725, 0.0331639448052763, 0.0593717259238862, 0.0586095568412675, 0.0710643594998291 , 0.0542769644148696, 0.0622404765975576, 0.0536064244218059, 0.0519102885973204, 0.0493863445475082, 0.0517804569480521, 0.0517211456806851, 0.0511253987894482, 0.0419591547767014, 0.0528999962423706, 0.06419336266174, 0.069237410318895, 0.0713884397366327, 0.0580538193950814, 0.084730114026892, 0.0769335955817974, 0.0855593629362882, 0.0519292899551365, 0.050876773415997, 0.0676511078249483, 0.0732312376684092, 0.0743946723794898, 0.0665503482658373, 0.0555488251322273, 0.0470531380177778 , 0.037777592584443, 0.0409234761554936, 0.0345834473195202, 0.0334 895212291953, 0.0277260994170984, 0.0265708838575095, 0.0271452389595483, 0.0259084599153654, 0.027959580475224, 0.0306489955753367, 0.0328760720029017, 0.0311483829721427, 0.0413979830020902, 0.047728997714206, 0.0515035254984261, 0.0421878234920752, 0.0418645252465591, 0.0434096272625613, 0.0521579392399781, 0.0514410097895711, 0.0499883145437549, 0.0404397864018975, 0.0425833520016323, 0.0486655254790584, 0.0564389639990789, 0.0556186336854241, 0.0630539085431343, 0.065666079347007, 0.070869476857214, 0.0615096987541091, 0.049069128261053, 0.0487439588352498, 0.0480084448188716, 0.0491650639209262, 0.0436654975228482, 0.0415799139247511, 0.039640484736356, 0.0382358642019319, 0.0345567696383646, 0.054637171859723, 0.0539703286358973, 0.051239025865198, 0.0304741735069425, 0.0276547408764417, 0.0277588467652429, 0.0269259048208619, 0.0304542870148651, 0.0378968781497573, 0.0385792550357231), SKEW = c(0.96728019756724, 2.42472216135109 , -0.21788331915771, -0.589261798446696, -0.549744 301636096, -0.0422495194778549, 0.0412380549826091, 0.35137501000572, 0.682869097532209, 0.782281964174801, -0.450361125171004, -0.488016712280699, 0.943430970115142, 2.61494605790314, 1.67840104658292, 0.0494001791565993, 0.504744823418497, 1.05389215830831, 1.22511769927417, 1.03801906089498, 0.505638715226445, 0.074791465001239, 0.368198217755297, 0.717926021653681, 0.471423996442527, 0.69552417891756, 0.926078921390832, 2.36266799955845 , 2.94477649644965, 0.640469883369529, 0.799507060035616, 0.265779764693551, 0.191606636855567, 0.0447592922353591, 0.00169280395714207, -0.13065910539762, -0.213861988500762, -0.184466027518926, 0.272899362222252, 0.392509260677987, 0.780935428471732, 0.681187297374058, 0.445637321564354, 0.477287511166656, 0.406893835763671, 0.345468287910708, 0.391817316521643, 0.605539416003897, 0.479926487469797, 0.369660204550789, -0.605369185234758 , -0.352414395767028, -0.240446750178277, -0.090164697152621, 0.0895116184929062, 0.0648045199933538, 0.0417585338 552771, -0.0341388270129196, 0.132806642274464, 0.255034672791302, 0.311955438741785, 0.380001462123998, 0.356764315563415, 0.305222472394173, 0.104992327972631, 0.0413874602372328, -0.034391920634866, 0.0559119034906321, -0.0421201193774125, 0.219703537578214, 0.30842629833452, 0.177409422251624, -0.371288947062746, 0.169037654477974, 0.330452704882459, 0.710518808997853, 0.387160218562557, -1.50906163140839, - 1.59114639387075, -1.85119669017341, 0.0163547882160698, 0.739007828574639, 0.703121528579507, 0.49400874385351, -0.282583144457125, -0.301598441316929, -0.262435057731434), RET12 = c(0.693147180559945, 0.750462469449239, 0.67116827384117, 0.559615787935423, 0.356903541493734, 0.278713402469021, 0.228841572428848, 0.356674943938732, 0.162571218446518, 0.100083458556983, -0.321583624127462, -0.405465108108164, -0.416893803931787, -0.944461608840852, -0.548565951748838, -0.259511195485085, 0.0392207131532814, -0.296877373096692, -0.488352767913932, -0.7339691750802, -0.76214005204 6897, -0.741937344729377, -0.371563556432483, -0.0741079721537219, -0.322773392263051, 0.0233444335897671, -0.367724780125317, 0.259511195485085, 0.352821374622742, -0.385662480811985, -0.318453731118535, -0.405465108108164, -0.287682072451781, -0.0512932943875504, -0.328504066972036, -0.422856850820033, 0.111225635110225, 0.171850256926659, 0.405465108108164, 0.53062825106217, 0.365459773494465, 0.462623521948113, 0.577315365034824, 0.611801541105993, 0.737598943130779, 0.596520344870874, 0.441832752279039, 0.666478933477784, 0.63907995928967, 0.62509371731493, 0.405465108108164, -0.103184236235231, -0.0631789016215316, -0.0571584138399488, -0.214409871345455, -0.248896047416624, -0.139761942375159, -0.263814591045137, -0.196710294246054, -0.49740260343385 , -0.587786664902119, -0.573800422927379, -0.405465108108164, -0.191055236762709, 0, 0.0194180858571016, 0.36101334553733, 0.231801614057324, -0.0339015516756813, 0.278203328497237, 0.36101334553733, 0.647684806483188, 1.011600911678 48, 1.06471073699243, 1.44036158239017, 1.46228026809781, 1.45962563420544, 0.997516171796741, 0.708185057924486, 0.923163611161917, 0.989412996703118, 0.814099790977608, 0.847297860387204, 0.669616683149751, 0.241162056816888, 0.0826917158451135, -0.23638877806423), RET.Next.12 = c(-0.416893803931787, -0.944461608840852, -0.548565951748838, - 0.259511195485085, 0.0392207131532814, -0.296877373096692, -0.488352767913932, -0.7339691750802, -0.762140052046897, -0.741937344729377, -0.371563556432483, -0.0741079721537219, -0.322773392263051, 0.0233444335897671, -0.367724780125317, 0.259511195485085, 0.352821374622742, 0.182467021918947, 0.169899036795397, 0.206200830583898, 0.153987401191905, 0.573687740522617, 0.821892136450508, 0.653926467406664 , 0.662982480513678, 0.803334139594701, 1.09861228866811, 0.555525802683897, 0.432133355190326, 0.462623521948113, 0.577315365034824, 0.611801541105993, 0.737598943130779, 0.596520344870874, 0.441832752279039, 0.666478933477784, 0.63907995928967, 0 .62509371731493, 0.405465108108164, -0.103184236235231, -0.0631789016215316, -0.0571584138399488, -0.214409871345455, -0.248896047416624, -0.139761942375159, -0.263814591045137, -0.196710294246054, -0.49740260343385, -0.587786664902119, -0.573800422927379, -0.405465108108164, -0.191055236762709, 0, 0.0194180858571016, 0.36101334553733, 0.231801614057324, -0.0339015516756813, 0.278203328497237, 0.36101334553733, 0.647684806483188, 1.01160091167848, 1.06471073699243, 1.44036158239017, 1.46228026809781, 1.45962563420544, 0.997516171796741, 0.708185057924486, 0.923163611161917, 0.989412996703118, 0.814099790977608, 0.847297860387204, 0.669616683149751, 0.241162056816888, 0.0826917158451135, -0.23638877806423, -0.158224005214894, -0.390427230743624, -0.0215062052209634 , -0.253448900809539, -0.440311839438333, -0.485507815781701, -0.25857398829371, -0.318453731118535, -0.319942934670002, -0.129458067236887, 0.0763729787845739, 0.0165293019512105), JACKPOT = structure(c(1L, 1L, 1L, 1L, 1L, 1L , 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L , 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L , 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L , 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("0", "1"), class = "factor")), row.names = c(NA, -87L), class = “数据帧”)

拟合数据

结构(列表(lpermno = C(10011L,10011L,10011L,10011L,10011L,10011L,10011L,10011L,10011L,10011L,10011L,10011L,10011L,10011L,10011L,10011L,10011L,10011L,10011L,10011L,10011L,10011L,10011L,10011L,10011L,10011L,10011L,10011L,10011L,10011L,10011L,10011L,10011L,10011L,,10011L,,10011L,,10011L,,,,地10011L, 10011L, 10011L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L, 10032L), year_mon = 结构(c(8401, 8432, 8460, 8491, 8521, 8552, 8582, 8613, 8644, 8674, 8705, 8735, 8766, 8797, 8825, 87856, 898788, 89886, , 9009, 9039, 9070, 9100, 8401, 8432, 8460, 8491, 8521, 8552, 8582, 8613, 8644, 8674, 8705, 8735, 8766, 8797, 8825, 8856, 8886, 8917, 8947, 8978, 9009 , 9039, 9070, 9100), class = c("yearmonth", "Date")), fyear = c(NA, NA, NA, NA, NA, NA, NA, 1992L, NA, NA, NA, NA, NA,NA,NA,NA,NA,NA,NA,1993L,NA,NA,NA,NA,NA,NA,1992L,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA, NA,1993L,NA,NA,NA,NA,NA,NA,NA,NA,NA),年龄 = c(31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L) , TANG = c(0.445286797310828, 0.445286797310828, 0.445286797310828, 0.445286797310828, 0.445286797310828, 0.445286797310828, 0.445286797310828, 0.468130690948045, 0.468130690948045, 0.468130690948045, 0.468130690948045, 0.468130690948045, 0.468130690948045, 0.468130690948045, 0.468130690948045, 0.468130690948045, 0.468130690948045, 0.468130690948045, 0.468130690948045, 0.306350215441125, 0.306350215441125, 0.306350215441125, 0.306350215441125 , 0.306350215441125, 0.404720423994572, 0.404720423994572, 0.38268276731165, 0.38268276731165, 0.38268276731165, 0.38268276731165, 0.38268276731165, 0.38268276731165, 0.38268276731165, 0.38268276731165, 0.38268276731165, 0.38268276731165, 0.38268276731165, 0.38268276731165, 0.349409872936132, 0.3494098729 36132, 0.349409872936132, 0.349409872936132, 0.349409872936132, 0.349409872936132, 0.349409872936132, 0.349409872936132, 0.349409872936132, 0.349409872936132), SALESGRTH = c(0.0134191878058735, 0.0134191878058735, 0.0134191878058735, 0.0134191878058735, 0.0134191878058735, 0.0134191878058735, 0.0134191878058735, 0.446944855631868, 0.446944855631868, 0.446944855631868, 0.446944855631868, 0.446944855631868, 0.446944855631868, 0.446944855631868 , 0.446944855631868, 0.446944855631868, 0.446944855631868, 0.446944855631868, 0.446944855631868, 0.477593112729782, 0.477593112729782, 0.477593112729782, 0.477593112729782, 0.477593112729782, 0.425509556825718, 0.425509556825718, 0.267535961557552, 0.267535961557552, 0.267535961557552, 0.267535961557552, 0.267535961557552, 0.267535961557552, 0.267535961557552, 0.267535961557552, 0.267535961557552, 0.267535961557552, 0.267535961557552, 0.267535961557552, 0.0140140412528342 , 0.0140140412528342, 0.0140140412528342, 0.0140140412528342, 0.014014041252834 2, 0.0140140412528342, 0.0140140412528342, 0.0140140412528342, 0.0140140412528342, 0.0140140412528342), date_endm.x = structure(c(8431, 8459, 8490, 8520, 8551, 8581, 8612, 8643, 8673, 8704, 8734, 8765, 8796, 8824, 8855 , 8885, 8916, 8946, 8977, 9008, 9038, 9069, 9099, 9130, 8431, 8459, 8490, 8520, 8551, 8581, 8612, 8643, 8673, 8704, 8734, 8765, 8796, 8824, 8855, 8885 , 8916, 8946, 8977, 9008, 9038, 9069, 9099, 9130), class = "Date"), TURN = c(-184218.671539397, -152974.427152573, -152974.427152573, -152974.427152573, 93134.2812416198, 93134.2812416198, 93134.2812416198, 86145.965441306, 86145.965441306 , 86145.965441306, 106711.84755538, 106711.84755538, 106711.84755538, 101530.802562158, 101530.802562158, 101530.802562158, -3275.13383666701, -3275.13383666701, -3275.13383666701, -16119.6522798106, -16119.6522798106, -16119.6522798106, -70585.6873004291, -70585.6873004291, -18408.3389599337, -18408.3389599337, -27815.1613434815, -27815.1613434815 , -27815.16134 34815, 49725.4727035562, 49725.4727035562, 49725.4727035562, 67472.1346437665, 67472.1346437665, 67472.1346437665, 62127.3303384468, 62127.3303384468, 62127.3303384468, 60780.2914249318, 60780.2914249318, 60780.2914249318, 25235.122368516, 25235.122368516, 25235.122368516, 7378.5314495312, 7378.5314495312, 7378.5314495312, -9850.0873716423 ), SIZE = c(3.14229171268543, 3.05267955399574, 3.05267955399574, 3.05267955399574, 3.15636366527262, 3.15636366527262, 3.15636366527262, 3.29558150006789, 3.29558150006789, 3.29558150006789, 3.69970502064847, 3.69970502064847, 3.69970502064847, 3.59616434170763, 3.59616434170763, 3.59616434170763, 4.12143064962841, 4.12143064962841, 4.12143064962841, 3.9350947929959, 3.9350947929959, 3.9350947929959, 4.51608484163236, 4.51608484163236, 4.57182020630654, 4.57182020630654, 4.7115821486817, 4.7115821486817, 4.7115821486817, 4.34867665499233, 4.34867665499233, 4.34867665499233, 4.58834950825775, 4.58834950825775, 4.58834950825775, 4.58958943402107, 4.58 958943402107, 4.58958943402107, 4.63821804003429, 4.63821804003429, 4.63821804003429, 4.32948255838468, 4.32948255838468, 4.32948255838468, 4.22883903260499, 4.22883903260499, 4.22883903260499, 4.00569548129078), SDEV = c(0.07566948223736, 0.0388458702984763, 0.0375929156315025, 0.0354348707389787, 0.0444705528105503, 0.0507645407251987, 0.0475974743453391, 0.0319104525820149, 0.0305352427654439, 0.0392707446746475, 0.0369223450049205 , 0.0343402111827153, 0.0255129852395989, 0.0260699375060244, 0.0282366046937742, 0.037831346324799, 0.0364400280270231, 0.0363042557129914, 0.0212188302075145, 0.0186592868960665, 0.0165394403086932, 0.0221356023063005, 0.0343660354570455, 0.0367256105166836, 0.0388527102641082, 0.0329309809592224, 0.0387913883726334, 0.0432490533407506, 0.0441107878102042, 0.0395354364086318, 0.034730906740653, 0.033147259298693, 0.0343984656074515, 0.0333975932519118, 0.0353031593181506, 0.0305569099546025 , 0.0305445479338893, 0.027875279634229, 0.0283823856517781, 0.03 67458476856234, 0.038601064665345, 0.0427126320353218, 0.0346599602603108, 0.037052674049311, 0.0375130865208876, 0.0385733004875974, 0.0429588255272181, 0.0402175466510754), SKEW = c(3.04309222693267, 0.484334850046598, 0.515565356117271, 0.229934964211078, 0.54028009537633, 1.3614908947775, 1.60053400736984, 0.316209052221715, 0.101251084370155, -0.0941280353319681, -0.165298146901818, -0.247477657726858, -0.0288953052101615, -0.18307327710416, 0.291963688227538, 0.677642428042238, 0.682903417951389, 0.80759277684452, 0.0943650757535768, -0.126596483703412, 0.533211157600825, 1.55725547595003, 2.03981715393004, 1.75855535310162, -0.0377152844441204, 0.252857011533151, -1.4011418726484, -1.74513849800184, -1.50274493203328, -0.794796720301789, 0.620574744428171, 0.710995520081692, 0.454793872329471, -0.0013284164175849, -0.126460229561597, 0.162546028792614, 0.517204257278072, 0.678271551304093, 0.372124301556777, -0.816926927542238, -0.720715101327707, -0.447521848611336, 0.175635942 263051, -0.0491426934943104, 0.0150951324411521, -0.0979269050892076, 0.234705962860257, 0.292078403674369), RET12 = c(0.182467021918947, 0.169899036795397, 0.206200830583898, 0.153987401191905, 0.573687740522617, 0.821892136450508, 0.653926467406664, 0.662982480513678, 0.803334139594701, 1.09861228866811, 0.555525802683897, 0.432133355190326, 0.492476485097794, 0.541597282432744, 0.835253044244263, 1.18351679575855 , 0.963179479076648, 0.625026846485818, 0.518793793415167, 0.62322842204923, 0.510825623765991, 0.702363835664869, 0.816379820983894, 0.824483182621032, -0.158224005214894, -0.390427230743624, -0.0215062052209634, -0.253448900809539, -0.440311839438333, -0.485507815781701, -0.25857398829371, -0.318453731118535, -0.319942934670002, -0.129458067236887, 0.0763729787845739, 0.0165293019512105 , 0, 0, -0.0752234212375877, 0.126293725324292, 0.17520408902509, -0.0210534091978323, -0.260726262463252, -0.169941365733582, -0.36136978824294, -0.405465108108165, -0.581921545449721, -0.5 8451333955715), RET.Next.12 = c(0.492476485097794, 0.541597282432744, 0.835253044244263, 1.18351679575855, 0.963179479076648, 0.625026846485818, 0.518793793415167, 0.62322842204923, 0.510825623765991, 0.702363835664869, 0.816379820983894, 0.824483182621032, 0.768872891088081, 0.656105908879596, 0.507880113536234, 0.55244729845681, 0.25489224962879, 0.34484048629173, 0.428995605518359, 0.456758402495715 , 0.405465108108164, 0.00913248356327268, -0.322083499169113, -0.324239668185578, 0, 0, -0.0752234212375877, 0.126293725324292, 0.17520408902509, -0.0210534091978323, -0.260726262463252, -0.169941365733582, -0.36136978824294, -0.405465108108165, -0.581921545449721, -0.58451333955715, -0.336472236621213, -0.312872321280339, - 0.246860077931526, -0.206336432997829, -0.154150679827258, 0.138836444854216, 0.354545017680907, 0.435049116146824, 0.447697871731437, 0.429562659687225, 0.581921545449721, 0.670841423045647), JACKPOT = structure(c(1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L , 1L, 1L, 1L , 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L , 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("0", "1"), class = (NA,)), row.names = c("0", "1"), -48L), class = "data.frame")

这是Find predictions for linear model 中方法的实现,它是 grouped_by ...

source("SO58578522_dat.R")  ## get data
library(tidyverse)
form <- JACKPOT ~ AGE+TANG+SALESGRTH+TURN+SIZE+SDEV+SKEW+RET12
model_results <-(model_data
    ## convert data to a list column
    %>% nest(data=-LPERMNO)
    ## fit model
    %>% mutate(model=map(data,
                         ~glm(form,
                              data=.,
                              family=binomial)),
              ## make predictions
               results=map(model,~predict(.,newdata=fit_data,
                        type="response"))
               )
    %>% unnest(cols=c(LPERMNO,results))
    %>% select(LPERMNO,results)
    %>% mutate(results=as.numeric(results>0.5))
)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM