Firth Logistic Regression in R

Hi All,

I am new to R...I want to run the Firth Logistic Regression Model in R as in my data set the split of 1 is 15% and 0 is 85%

..Can you please let me know the code to go about the same...Also attahed a sample of the data

Thanks,
Arun

Hi Arun, welcome!

I just want to point out to you that you are not actually providing sample data but a picture of it, and that is not very useful, so can you please share a small part of the data set in a copy-paste friendly format?

Since the data set is in a spreadsheet, check out the datapasta package to properly share sample data.

1 Like

tibble::tribble(
~Itemid, ~AverageSales, ~PercentageofPromo, ~Availability, ~BuyBox, ~Salesoutput,
1, 4774.8575, 0, 1, 57.6785714285714, 0,
2, 2109.18375, 0, 1, 25.8928571428571, 1,
3, 1987.97125, 0, 1, 29.6607142857143, 0,
4, 1678.125, 0, 1, 29.4464285714286, 0,
5, 1408.35125, 0, 1, 93.0535714285714, 1,
6, 1423.4575, 0, 1, 80.2321428571429, 0,
7, 1117.72125, 0, 1, 86.2142857142857, 1,
8, 1144.89125, 0, 1, 51.5357142857143, 0,
9, 1068.50875, 0, 1, 29.3392857142857, 0,
10, 1106.81375, 0, 1, 31.7857142857143, 0,
11, 879.715, 0, 1, 32.1428571428572, 1,
12, 1042.28125, 0, 1, 33.7678571428571, 0,
13, 1031.5525, 0, 1, 62.3571428571428, 0,
14, 1018.57375, 0, 1, 79.5, 0,
15, 934.38875, 0.753016343572202, 0.928571428571429, 15.4285714285714, 0,
16, 946.72875, 0, 1, 46.0714285714286, 0,
17, 808.31125, 0, 0.70187251984127, 26.2678571428571, 0,
18, 710.68, 0, 1, 51.0178571428572, 0,
19, 660.6175, 0, 1, 53.6428571428571, 1,
20, 668.6275, 0, 1, 11.8214285714286, 0,
21, 642.8575, 0.750844860641744, 0.973214285714286, 20.2142857142857, 1,
22, 628.27875, 0.481806443397934, 1, 22.6607142857143, 0,
23, 628.1475, 0, 1, 88.6607142857143, 0,
24, 678.15625, 0, 1, 19.6785714285714, 0,
25, 516.94125, 0, 1, 56.8035714285714, 1,
26, 609.52625, 0, 0.933568948412698, 75.125, 0,
27, 587.125, 0, 0.986768353174603, 46.2040816326531, 0,
28, 555.89125, 0, 1, 7.91071428571429, 0,
29, 583.31625, 0, 1, 32.2857142857143, 0,
30, 512.335, 0, 1, 88.0535714285714, 0,
31, 532.91375, 0, 0.924317956349206, 6.85714285714286, 0,
32, 503.02125, 0, 0.896502976190476, 18.5918367346939, 0,
33, 425.84875, 0.340930905632575, 1, 15.0714285714286, 1,
34, 471.4925, 0, 1, 54.3392857142857, 0,
35, 385.8675, 0, 0.947172619047619, 19.7857142857143, 1,
36, 458.73, 0, 1, 21.5535714285714, 0,
37, 438.3375, 0, 1, 21.93, 0,
38, 382.9575, 0, 1, 83.0178571428572, 1,
39, 384.00125, 0, 0.763318452380952, 34.1071428571429, 0,
40, 424.23625, 0.363049480095112, 0.720833333333333, 59.9285714285714, 0,
41, 370.615, 0.48571968214994, 1, 3.73214285714286, 0,
42, 341.5775, 0, 1, 85.3571428571429, 1,
43, 328.1225, 0.61787137425809, 1, 23.6428571428572, 1,
44, 388.09, 0, 1, 23.6607142857143, 0,
45, 285.345, 0, 1, 19.3392857142857, 1,
46, 338.2475, 0, 1, 63.125, 1,
47, 302.90625, 0, 1, 88.5714285714286, 1,
48, 341.7575, 0, 1, 12.7321428571429, 0,
49, 331.03, 0, 0.915091765873016, 44.3333333333333, 0,
50, 324.31125, 0, 0.880543154761905, 59.8857142857143, 0,
51, 255.18, 0, 1, 16.9285714285714, 1,
52, 240.0525, 0, 1, 36.4821428571429, 1,
53, 318.885, 0, 1, 52.5535714285714, 0,
54, 316.4, 0, 0.856113591269841, 31.8571428571429, 0,
55, 255.7875, 0, 1, 90.7857142857143, 1,
56, 286.9725, 0, 1, 6.23214285714286, 0,
57, 303.2675, 0.0748060705482784, 1, 21.93, 0,
58, 223.51, 0, 1, 7.125, 1,
59, 296.0775, 0, 1, 42.9642857142857, 0,
60, 146.6725, 0, 1, 88.8928571428571, 1,
61, 289.74875, 0, 1, 13.125, 0,
62, 250.57375, 0, 1, 9.125, 1,
63, 214.8475, 0, 1, 13.2321428571429, 1,
64, 283.845, 0, 0.897929067460317, 25.5, 0,
65, 279.50125, 0, 1, 26.7857142857143, 0,
66, 270.96375, 0, 1, 18.3928571428571, 0,
67, 270.885, 0, 1, 77.8392857142857, 0,
68, 268.50875, 0, 1, 42.25, 0,
69, 258.3825, 0, 1, 67.3392857142857, 0,
70, 252.54625, 0.536797715269975, 1, 46.7857142857143, 0,
71, 251.69125, 0, 1, 42.3214285714286, 0,
72, 226.2325, 0, 1, 15.6071428571429, 0,
73, 242.975, 0, 0.897929067460317, 77.2857142857143, 0,
74, 242.35375, 0, 0.933457341269841, 81.2857142857143, 0,
75, 240.49, 0, 0.892075892857143, 19.2, 0,
76, 238.3375, 0, 0.943625992063492, 11.125, 0,
77, 225.40875, 0, 1, 81.5, 0,
78, 157.49125, 0, 1, 16.0892857142857, 1,
79, 222.21, 0, 1, 6.25, 0,
80, 215.70625, 0, 1, 24.8035714285714, 0,
81, 214.77, 0, 1, 39.9821428571428, 0,
82, 209.99875, 0, 0.876860119047619, 37.8095238095238, 0,
83, 82.30375, 0, 1, 2.71428571428571, 1,
84, 206.68875, 0.649364080047898, 0.957366071428571, 22.9285714285714, 0,
85, 197.96, 0, 0.96796875, 65.5714285714286, 0,
86, 197.70125, 0.49192278753928, 1, 21.93, 0,
87, 193.9575, 0, 1, 2.85714285714286, 0,
88, 192.12625, 0, 1, 8.26785714285715, 0,
89, 187.0475, 0.0711116160333605, 1, 11.0714285714286, 0,
90, 48.83, 0, 0.908606150793651, 3.83928571428572, 1,
91, 180.8625, 0.11141060197664, 0.897929067460317, 64.9523809523809, 0,
92, 180.5775, 0, 1, 65.6071428571429, 0,
93, 180.28625, 0.902620138807036, 0.924578373015873, 11.9047619047619, 0,
94, 177.08, 0, 1, 2.21428571428571, 0,
95, 175.2075, 0, 1, 12.7142857142857, 0,
96, 70.62125, 0, 1, 1.08928571428571, 1,
97, 171.095, 0, 1, 25.8928571428572, 0,
98, 85.03875, 0, 1, 2.26785714285714, 1,
99, 164.72875, 0, 1, 21.93, 0,
100, 148.86125, 0.778745308130894, 1, 19.6071428571428, 0,
101, 162.46625, 0.231648111530856, 1, 12.3035714285714, 0,
102, 162.165, 0, 0.766369047619048, 13.1071428571428, 0,
103, 162.09125, 0, 0.809461805555556, 7.71428571428571, 0,
104, 158.7025, 1, 0.96796875, 13.7142857142857, 0,
105, 157.5225, 0, 0.950037202380952, 10.7619047619048, 0,
106, 152.93875, 0, 0.927914186507936, 7.93877551020408, 0,
107, 151.27875, 0, 1, 6.03571428571427, 0,
108, 125.5375, 0, 0.993675595238095, 29.7619047619048, 1,
109, 144.57625, 0, 1, 31.8571428571428, 0,
110, 142.03125, 0, 0.924107142857143, 3.04081632653062, 0,
111, 138.99125, 0.36120978838596, 1, 47.7142857142857, 0,
112, 138.8025, 0, 0.89656498015873, 18.5357142857143, 0,
113, 137.08125, 0, 1, 11.9464285714286, 0,
114, 67.57875, 0, 0.938913690476191, 7.92857142857142, 1,
115, 136.37625, 0, 1, 25.9821428571429, 0,
116, 115.94, 0, 1, 19.75, 1,
117, 86.1675, 0, 1, 13.0892857142857, 1,
118, 129.12625, 0, 0.882936507936508, 12.2321428571428, 0,
119, 78.29625, 0, 0.89360119047619, 28.3571428571429, 1,
120, 125.44, 0, 0.982167658730159, 26.9642857142857, 0,
121, 123.6175, 0, 0.880543154761905, 15.6571428571428, 0,
122, 122.25, 0, 0.615277777777778, 10.8928571428571, 0,
123, 120.97875, 0, 1, 11.7142857142857, 0,
124, 120.93375, 0, 0.773263888888889, 25.0857142857143, 0,
125, 117.4175, 0, 1, 6.96428571428572, 0,
126, 116.8925, 0, 0.935974702380952, 14.1607142857143, 0,
127, 113.80125, 0, 1, 16.4821428571429, 0,
128, 112.495, 0, 1, 5.83928571428572, 0,
129, 111.85625, 0, 0.839794146825397, 44.6428571428572, 0,
130, 89.0875, 0, 1, 12.125, 1,
131, 110.94625, 0.337505774192458, 1, 89.5357142857143, 0,
132, 86.87875, 0, 1, 0, 1,
133, 107.385, 0, 0.928645833333333, 3.87755102040816, 0,
134, 106.87, 0, 1, 5.82142857142857, 0,
135, 105.8, 0, 1, 0.964285714285714, 0,
136, 105.51375, 0, 1, 10.375, 0,
137, 104.9275, 0, 0.928645833333333, 58.6071428571429, 0,
138, 104.29875, 0, 0.856051587301587, 9.10714285714287, 0,
139, 103.86125, 0, 1, 3.53571428571429, 0,
140, 102.5825, 0.66710939975142, 1, 16.6666666666667, 0,
141, 76.54, 0, 0.968030753968254, 30.9821428571429, 1,
142, 101.24625, 0, 1, 0.607142857142858, 0,
143, 101.1575, 0, 1, 0.732142857142858, 0,
144, 100.005, 0, 1, 90.5, 0,
145, 98.78625, 0, 0.96796875, 54.3214285714286, 0,
146, 97.3075, 0, 1, 28.1785714285714, 0,
147, 95.58125, 0, 1, 20.7321428571429, 0,
148, 94.64375, 0, 1, 8.14285714285714, 0,
149, 93.18125, 0, 1, 45.7142857142857, 0,
150, 91.05875, 0, 1, 17.1071428571429, 0,
151, 91.05375, 0, 1, 9.64285714285715, 0,
152, 91.01875, 0, 1, 12.7678571428571, 0,
153, 78.1825, 0, 1, 13.875, 1,
154, 89.61875, 0, 1, 0, 0,
155, 89.06875, 0, 0.873871527777778, 23.7857142857143, 0,
156, 88.39, 0, 1, 26.7142857142857, 0,
157, 88.03875, 0.638525649216964, 1, 9.92857142857143, 0,
158, 57.86125, 0, 1, 18.625, 1,
159, 86.66125, 0.343132022674494, 1, 58.0892857142857, 0,
160, 86.43625, 0, 1, 85.6607142857143, 0,
161, 28.76875, 0, 1, 29.2857142857143, 1,
162, 50.0175, 0, 1, 22.1071428571429, 1,
163, 82.55375, 0, 1, 34.6785714285714, 0,
164, 82.245, 0, 1, 40.5357142857143, 0,
165, 80.6775, 0, 0.833643353174603, 9.52380952380953, 0,
166, 79.80375, 0, 0.994047619047619, 43.1607142857143, 0,
167, 79.3475, 0, 0.862636408730159, 16.9285714285714, 0,
168, 26.97375, 0, 0.948883928571429, 48.9285714285714, 1,
169, 78.8025, 0, 1, 5.7857142857143, 0,
170, 78.21875, 0, 0.893415178571429, 37.5, 0,
171, 77.9575, 0, 0.96796875, 27.6785714285714, 0,
172, 76.9475, 0, 0.888045634920635, 52.25, 0,
173, 76.69625, 0, 0.982167658730159, 66.9821428571428, 0,
174, 76.2225, 0, 0.96391369047619, 69, 0,
175, 75.91375, 0, 0.89468005952381, 5.48214285714286, 0,
176, 62.95625, 0, 1, 51.7857142857143, 1,
177, 72.72, 0, 1, 89.3214285714286, 0,
178, 72.5025, 0, 1, 0.821428571428571, 0,
179, 72.03375, 0, 0.804005456349206, 5.71428571428571, 0,
180, 59.78125, 0.190820700470465, 1, 55.8214285714286, 1,
181, 71.6125, 0, 0.883680555555556, 8.11904761904763, 0,
182, 70.86, 0, 0.693526785714286, 32.5476190476191, 0,
183, 70.81875, 0, 1, 26.0357142857143, 0,
184, 55.705, 0, 1, 7.82142857142856, 1,
185, 70.57625, 0, 0.951984126984127, 14.5357142857143, 0,
186, 34.79125, 0, 0.964285714285714, 27.9183673469388, 1,
187, 69.32375, 1, 1, 12.8035714285714, 0,
188, 68.7775, 0, 1, 7.96428571428571, 0,
189, 68.73375, 0, 0.986768353174603, 17.1428571428572, 0,
190, 41.50125, 0, 0.784002976190476, 1.92857142857143, 1,
191, 68.14, 0, 0.964273313492063, 4.26785714285715, 0,
192, 66.3875, 0, 1, 0, 0,
193, 66.2475, 0, 1, 13.6964285714286, 0,
194, 66.24, 0, 1, 19.0178571428571, 0,
195, 65.78625, 0, 1, 14.8928571428572, 0,
196, 64.9975, 0, 1, 0.589285714285714, 0,
197, 64.265, 0, 1, 0.535714285714286, 0,
198, 63.5575, 0, 1, 33.25, 0,
199, 60.96, 0, 1, 54.5535714285714, 0
)

Here is a blog post that discusses Firth Logistic regression


and a link to the CRAN page from the logistf package:
https://cran.r-project.org/web/packages/logistf/index.html

Can anyone please provide me with a Sample code in R to run the Firth Logistic regression for the above data set..

Thanks,
Arun

I have never used the logistf package that is used in the blog post I linked but looking at the documentation, something like this should work. I have truncated the code for defining df simply to make the code more legible.

df <- tibble::tribble(
  ~Itemid, ~AverageSales, ~PercentageofPromo, ~Availability, ~BuyBox, ~Salesoutput,
  1, 4774.8575, 0, 1, 57.6785714285714, 0,
  2, 2109.18375, 0, 1, 25.8928571428571, 1,
  3, 1987.97125, 0, 1, 29.6607142857143, 0,
...)
library(logistf)
FIT = logistf(Salesoutput ~ AverageSales + PercentageofPromo + Availability + BuyBox, 
              data = df)
summary(FIT)
1 Like

Thanks!..Really appreciate your help!..

If your question's been answered (even if by you), would you mind choosing a solution? (See FAQ below for how).

Having questions checked as resolved makes it a bit easier to navigate the site visually and see which threads still need help.

Thanks

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