R code for logit generates values greater than 1? -
the logistic transformation supposed generate values in (0,1). if @ following logistic transformation, see creates values greater 1. going wrong?
lambda1=0 out=matrix(na,400,1) (i in 1:400){ lambda1[i+1]=((exp(0.8*lambda1[i]+rnorm(1)))/(1+exp(0.8*lambda1[i]+rnorm(1)))) out[i]=lambda1[i] }
each time call rnorm(1)
getting different random draw, random value in numerator , denominator may different.
note exp(x) / (1+exp(x))
equivalent 1 / (1 + exp(-x))
, instead do:
lambda1=0 out=matrix(na,400,1) (i in 1:400){ lambda1[i+1]=(1/(1+exp(-(0.8*lambda1[i]+rnorm(1))))) out[i]=lambda1[i] } summary(lambda1) # min. 1st qu. median mean 3rd qu. max. # 0.0000 0.4523 0.6352 0.6119 0.7719 0.9682
note: large vectors, find more efficient preallocate lambda1
, compute out
@ end in 1 shot (i'll assume want elements 2 through 401 in out
instead of elements 1 through 400):
lambda1 <- rep(0, 401) (i in 1:400) lambda1[i+1]=(1/(1+exp(-(0.8*lambda1[i]+rnorm(1))))) out <- matrix(tail(lambda1, -1))
Comments
Post a Comment