Predictions in logistic regression model by group

Hi all,
this might possibly be a duplicate question but I can't find any answer that works for my problem.

I am running a logistic regression model in R for each stock (=LPERMNO), which is why I group my dataset before running glm. However, I get the following error when I try to make predictions:

no applicable method for 'predict' applied to an object of class "c('rowwise_df', 'tbl_df', 'tbl', 'data.frame')

My goal is to receive a table with predicted probabilities for JACKPOT (=variable =1, not Jackpot=0) for each stock (LPERMNO).

I wrote the following code:


# 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)

Please find the dput output for model_data & fit_data below

model_data:

structure(list(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,
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,
7974, 8004, 8035, 8066, 8095, 8126, 8156, 8187, 8217, 8248, 8279,
8309, 8340, 8370, 6634, 6665, 6695, 6726, 6756, 6787, 6818, 6848,
6879, 6909, 6940, 6971, 6999, 7030, 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", "Date"
)), 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, NA, 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, NA, NA,
NA, NA, NA), AGE = 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, 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,
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.120193238812454, 0.120193238812454, 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, 6725, 6755, 6786,
6817, 6847, 6878, 6908, 6939, 6970, 6998, 7029, 7059, 7090, 7120,
7151, 7182, 7212, 7243, 7273, 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 = "Date"),
TURN = 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.58267592475001,
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.7209178182824,
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.0334895212291953, 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.549744301636096,
-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.0417585338552771, -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.762140052046897,
-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.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), 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 = "data.frame")

fit_data

structure(list(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, 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(8401, 8432, 8460,
8491, 8521, 8552, 8582, 8613, 8644, 8674, 8705, 8735, 8766, 8797,
8825, 8856, 8886, 8917, 8947, 8978, 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, 1993L, NA, NA, NA, NA, NA, NA, NA, NA,
NA), AGE = 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.349409872936132,
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.0140140412528342,
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.1613434815, 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.58958943402107, 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.0367458476856234, 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.175635942263051, -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.58451333955715), 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 = "factor")), row.names = c(NA,
-48L), class = "data.frame")

1 Like

Hi,

The root problem is that predict takes a model object, such as this one from help(glm)

utils::data(anorexia, package = "MASS")
anorex.1 <- glm(Postwt ~ Prewt + Treat + offset(Prewt), family = gaussian, data = anorexia)
summary(anorex.1)
#> 
#> Call:
#> glm(formula = Postwt ~ Prewt + Treat + offset(Prewt), family = gaussian, 
#>     data = anorexia)
#> 
#> Deviance Residuals: 
#>      Min        1Q    Median        3Q       Max  
#> -14.1083   -4.2773   -0.5484    5.4838   15.2922  
#> 
#> Coefficients:
#>             Estimate Std. Error t value Pr(>|t|)    
#> (Intercept)  49.7711    13.3910   3.717 0.000410 ***
#> Prewt        -0.5655     0.1612  -3.509 0.000803 ***
#> TreatCont    -4.0971     1.8935  -2.164 0.033999 *  
#> TreatFT       4.5631     2.1333   2.139 0.036035 *  
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> (Dispersion parameter for gaussian family taken to be 48.69504)
#> 
#>     Null deviance: 4525.4  on 71  degrees of freedom
#> Residual deviance: 3311.3  on 68  degrees of freedom
#> AIC: 489.97
#> 
#> Number of Fisher Scoring iterations: 2

Created on 2019-10-27 by the reprex package (v0.3.0)

As you can see from str(my_model) compared to `str(your_model), these are completely different beasts.

What I suggest is to pick one stock, develop a model, and then to use purrr to map it to the entire dataset.

I'm currently writing a logistic regression in R primer; I'll post an announcement next month when it's complete.

1 Like

You can do something like this

library(tidyverse)

model_data %>%
    group_nest(LPERMNO) %>% 
    mutate(model = map(data,
                       ~glm(JACKPOT ~ AGE+TANG+SALESGRTH+TURN+SIZE+SDEV+SKEW+RET12,
                            data = .x,
                            family = "binomial"))
           ) %>% 
    left_join(fit_data %>% group_nest(LPERMNO), by = "LPERMNO") %>% 
    mutate(fitted = map2(model,
                         data.y,
                         ~predict(.x, newdata = .y, type = "response"))) %>% 
    unnest_longer("fitted") %>% 
    select(LPERMNO, fitted)
#> # A tibble: 48 x 2
#>    LPERMNO    fitted
#>      <int>     <dbl>
#>  1   10011  1.29e- 1
#>  2   10011 10.00e- 1
#>  3   10011 10.00e- 1
#>  4   10011 10.00e- 1
#>  5   10011  2.22e-16
#>  6   10011  2.22e-16
#>  7   10011  2.22e-16
#>  8   10011 10.00e- 1
#>  9   10011 10.00e- 1
#> 10   10011 10.00e- 1
#> # … with 38 more rows
1 Like

Hi @technocrat. Thank you for the input. I indeed wasn't aware of the difference here! Maybe @andresrcs answer can also help you with your regression problem.

1 Like

This is perfect - many thanks @andresrcs!!! I couldn't get it to work the way you did.

Two more small questions:

  1. is it easily possibly to adjust the code so that the output also includes the date of the fit_data = year_mon?
  2. when applying the code on my complete dataset, I noticed that for cases where there was no matching LPERMNO in the fit data but where a model was created from the model input (i.e. a stock is included in the model_data but not in the fit_data), the output still includes some fit values which are probably based on empty data. It's probably easiest to just exclude these stocks from both data sets in the first place, or can we adjust the "left_join" for that?
    thanks for your help!
1 Like

This code solves that part

unnest(c("fitted", "data.y")) %>% 
    select(LPERMNO, year_mon, fitted)

I think using inner_join() would solve this issue

So this would be the new solution

library(tidyverse)

model_data %>%
    group_nest(LPERMNO) %>% 
    mutate(model = map(data,
                       ~glm(JACKPOT ~ AGE+TANG+SALESGRTH+TURN+SIZE+SDEV+SKEW+RET12,
                            data = .x,
                            family = "binomial"))
    ) %>% 
    inner_join(fit_data %>% group_nest(LPERMNO), by = "LPERMNO") %>% 
    mutate(fitted = map2(model,
                         data.y,
                         ~predict(.x, newdata = .y, type = "response"))) %>% 
    unnest(c("fitted", "data.y")) %>% 
    select(LPERMNO, year_mon, fitted)
#> # A tibble: 48 x 3
#>    LPERMNO year_mon      fitted
#>      <int> <fct>          <dbl>
#>  1   10011 1993-01-01  1.29e- 1
#>  2   10011 1993-02-01 10.00e- 1
#>  3   10011 1993-03-01 10.00e- 1
#>  4   10011 1993-04-01 10.00e- 1
#>  5   10011 1993-05-01  2.22e-16
#>  6   10011 1993-06-01  2.22e-16
#>  7   10011 1993-07-01  2.22e-16
#>  8   10011 1993-08-01 10.00e- 1
#>  9   10011 1993-09-01 10.00e- 1
#> 10   10011 1993-10-01 10.00e- 1
#> # … with 38 more rows
1 Like

Greatly appreciate your magic @andresrcs! Thank you, it works perfectly.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.