An even simpler code is to use directly a poisson regression with
library(tidyverse)
library(broom)
d <- data.frame(g=factor(1:2),
s=c(25,75),
f=c(38,162))
d_long = pivot_longer(d, cols = 2:3)
tidy(glm(value ~ name*g, data=d_long, family = 'poisson'))
Some explanations
If you look at the poisson model, you see that it models the probability $\pi$ as
\begin{equation}
\ln(\pi_{i}) = \mathbf{X_{i}} \beta
\end{equation}
the log of probabilities is a simple linear combinations of predictors. This is the same equation as the logistic equation which is called "logit"
\begin{equation}
\text{logit}(\pi_{i}) = \ln \left( \frac{\pi_{i}}{1 - \pi_{i}} \right) = \mathbf{X_{i}} \beta
\end{equation}
You can test this equivalence with a simple model. You start with this linear model
\begin{equation}
y_{i} = 2u_{i} + \epsilon_{i}
\end{equation}
set.seed(123)
n = 100
u = rbinom(n, 1, 0.2)
y = u*2 + rnorm(n, 0, 0.5)
y_prob = plogis(y) # transform log(odds) into a proba
y_bin = rbinom(n, 1, prob = y_prob)
You can directly model the data with a logistic regression
glm(y_bin ~ u, family = "binomial")
Coefficients:
(Intercept) u
-0.09764 2.93085
This is the log of odds, which we can simply retrieve with a table
data.frame(table(u, y_bin)) %>% mutate(p = Freq/sum(Freq))
$$
\begin{aligned}[ht]
\begin{array}{rllrr}
\hline
& u & y\_bin & Freq & p \\
\hline
1 & 0 & 0 & 43 & 0.43 \\
2 & 1 & 0 & 1 & 0.01 \\
3 & 0 & 1 & 39 & 0.39 \\
4 & 1 & 1 & 17 & 0.17 \\
\hline
\end{array}
\end{aligned}
$$
The coefficient of your logistic regression is the log odds-ratio of this table
\begin{equation}
\ln \left( \frac{\frac{0.17}{0.39}}{\frac{0.01}{0.43}} \right) = 2.930852
\end{equation}
log((0.17/0.39) / (0.01/0.43))
2.930852
This is exactly the same as modelling the table directly with a poisson regression
Just prepare the table
df_poiss = data.frame(table(u, y_bin))
Run the poisson, and the interaction will give you the log odds-ratio between the outcome and the predictor u
glm(Freq ~ y_bin*u, data = df_poiss, family = "poisson")
Coefficients:
(Intercept) y_bin1 u1 y_bin1:u1
3.76120 -0.09764 -3.76120 2.93085
The problem is that you can always transform dummy variables into counts for the purpose of using a poisson model. But you often cannot use aggregated counts to revert back to dummies for using logistic regression.
A final note if you look closely at the two equations, the logistic model (the logit transformation) once exponentiate will give you odds, while when exponentiating the poisson, will give you a probability (or risk). However, when you take an interaction with the poisson, then you have a ratio of odds, which is the same as the logistic model.