I want to plot the loglikelihood function of a series of independent Bernoulli distributed random variables y with parameter p being a function (the logistic function) of some feature x. This logistic function also has a parameter b. This is the parameter I want to estimate. So I want to plot the loglikelihood as a function of b. I want to do so in R using ggplot2, because I want to become better in those.
My creation of the loglikelihood function can and should be done better, but that is not my point here. The problem is that the plotted loglikelihood is constant over the interval (-5,5). That seems wrong. Especially, because when I call the function with some arbitrary b in that interval it returns a different value. Why does this happen? Thank you.
library(ggplot2)
set.seed(123)
# parameters
n=100
mu=0
s=2
b<-0.2
# functions
logit <- function(x,b){1/(1+exp(-b*x))}
# simulation of data
x<-rnorm(n,mu,s)
y_prob<-logit(x,b)
y<-rbinom(n,1,y_prob)
df<-data.frame(x,y)
# loglikelihood function
loglikelihood<-function(b,df){
prd<-1
for (i in 1:NROW(df)){
events<-logit(df$x[i],b)
nonevents<-1-events
prd<-prd*events^df$y[i]*nonevents^(1-df$y[i])
}
return(sum(log(prd)))
}
loglikelihood(0.3,df)
p2<-ggplot(data=data.frame(b=c(-5,5)), aes(b)) + stat_function(fun=loglikelihood, args=list(df=df))
p2<-p2+xlab("b") + ylab("loglikelihood")
p2
