Z. Y. Jeng
Z. Y. Jeng

Reputation: 55

Logistic model solution in r

I have fit a logistic model and I want to know the value of x when y=0.5.

library("faraway")

data = read.table("SBay.txt", header=T)
female = subset(data,sex==2)

amat = c(0,1)
mod1 = glm(amat[maturity]~age,family=binomial,data=female)
summary(mod1)
(len_50 = (log(1)-mod1$coef[1]) /mod1$coef[2])

(plot(female$age,amat[female$maturity],pch=1,col=2))
s = seq(0, 20, 0.1)
lines(s,ilogit(mod1$coef[1]+mod1$coef[2]*s),col=2,lty=2,lwd=2)

I google the answer and get:

F1 = ilogit(mod1$coef[1]+mod1$coef[2])
F2 = 0.5
solve(F1,F2)

I got the answer equal to 9.931613, however, it doesn't match the graph I've plot. SO I think there might be something wrong with my code. Here are my graph, it seem the answer is between 2 and 3.

https://i.sstatic.net/4qrAk.jpg

Upvotes: 0

Views: 148

Answers (1)

hello_there_andy
hello_there_andy

Reputation: 2083

What you need is the predict function followed by an ifelse expression:

# Logistic Regression Classifier
mod1 = glm( amat[maturity]~age,family=binomial,data=female)

# Probabilities 
prob_pred = predict( mod1, type = "response", newdata = female) # probability outcomes

# Outcomes (True/False)
y_pred = ifelse( prob_pred > 0.5, 1, 0)  # NOTE: the 0.5, it filters outcomes > 0.5 probability

Upvotes: 1

Related Questions