Im trying to plot the marginal effect of non wife income (nwifeinc) across the range of non-wife income, holding the values of other variables fixed at their sample means. holding is the logistic regression I made. But I dont know what function I should use other than dlogis to get a graph. Also, there is an error for object of type 'closure' is not subsettable. So, there aren't any points on my graph. Sorry if that is confusing.
h1<-rep(0,nrow(mroz))
for(i in 1:nrow(mroz)){
holding<-{
logit <- glm(inlf ~ nwifeinc + educ + age + exper +
kidsge6 + kidslt6 +unem+ city,
data = sample, family = binomial(link="logit"))
X <- sample %>% select(nwifeinc, educ, age, exper, kidslt6, kidsge6, unem, city)
ones <- rep(1, nrow(X))
X <- cbind(ones, X)
X_means <- colMeans(X)
betas <- logit$coefficients
linear_part <- X_means %*% betas
PEA <- logit$coefficients[2] * (exp(linear_part)/((1 + exp(linear_part))^2))
}
h1[i]<-dlogis(holding)*data$nwifeinc[i]
}
h1_df<-as.data.frame(h1)
ggplot(data=h1_df, aes(x=h1))+
geom_density()+
xlab("Marginal Effect of Non Wife Income")+
ylab("")