'two-sample proportion contrast function?

I am trying to implement a function in r that allows me to calculate a proportion contrast for two samples at 95% without using the prop.test function.

The context is that I have a dataset in which I want to determine if the proportion of "managers" is lower in women than in men. For this I have several columns, including one that is gender (masculine/feminine) and category(manager,subordinate).

I have tried to implement the following code but I don't get correct results after checking with prop.test

x1 <- df[df$gender=="masculine",] 
x2 <- df[df$gender=="femenine",]
p1 <- sum(x1$category=="manager")/length( x1$category ); p1
p2 <- sum(x2$category=="manager")/length( x2$category ); p2

p<-(n1*p1 + n2*p2) / (n1+n2)
zobs <- (p1-p2)/( sqrt(p*(1-p)*(1/n1+1/n2)) )
zcrit <- qnorm(0.02, lower.tail=FALSE)
pvalue<- pnorm(zobs, lower.tail=FALSE)

c(zobs, zcrit, pvalue)

Any idea what I might be doing wrong or any coding suggestions?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source