I have following code:
loglikelihood <- function(alpha,beta= 0.1){
loglh<-0
d<-0
p<-0
k<-NULL
data<-read.table("epidemic.txt",header = TRUE)
attach(data, warn.conflicts = F)
k <-which(inftime==1)
d <- (sqrt((x-x[k])^2+(y-y[k])^2))^(-beta)
p<-1 - exp(-alpha*d)
for(i in 1:100){
if(i!=k){
if(inftime[i]==0){
loglh<-loglh +log(1-p[i])
}
if(inftime[i]==2){
loglh<-loglh + log(p[i])
}
}
}
return(loglh)
}
Here p[i] <- 1 - exp(-alpha*d^(-beta))
so, log(p[i]) produces NaNs when exp(-alpha*d^(-beta)) is greater than 1.
How can I remove it.After generating the out put we can omit it, but the
problem is different.
On Tue, Nov 29, 2011 at 5:22 PM, Gyanendra Pokharel <
gyanendra.pokharel@gmail.com> wrote:
> No, that,s not a problem Michael,
> I have following code:
> loglikelihood <- function(alpha,beta= 0.1){
> loglh<-0
> d<-0
> p<-0
> k<-NULL
> data<-read.table("epidemic.txt",header = TRUE)
> attach(data, warn.conflicts = F)
> k <-which(inftime==1)
> d <- (sqrt((x-x[k])^2+(y-y[k])^2))^(-beta)
> p<-1 - exp(-alpha*d)
> for(i in 1:100){
> if(i!=k){
> if(inftime[i]==0){
> loglh<-loglh +log(1-p[i])
> }
> if(inftime[i]==2){
> loglh<-loglh + log(p[i])
> }
> }
> }
> return(loglh)
> }
> Here p[i] <- 1 - exp(-alpha*d^(-beta))
> so, log(p[i]) produces NaNs when exp(-alpha*d^(-beta)) is greater than 1.
> How can I remove it.After generating the out put we can omit it, but the
> problem is different.
>
>
>
> On Tue, Nov 29, 2011 at 5:07 PM, R. Michael Weylandt <
> michael.weylandt@gmail.com> wrote:
>
>> Do you mean remove the NaNs? Try na.omit() or complete.cases() or many
>> other options.
>>
>> If you mean you want the complex log, try
>>
>> log(as.complex(1-exp(-a)))
>>
>> Michael
>>
>> On Tue, Nov 29, 2011 at 5:02 PM, Gyanendra Pokharel
>> <gyanendra.pokharel@gmail.com> wrote:
>> > Hi all I have a function of log defined by y = log(1- exp(-a)),
when
>> > exp(-a) is greater, 1, it produce NaN. How can I remove this in R?
>> >
>> > [[alternative HTML version deleted]]
>> >
>> > ______________________________________________
>> > R-help@r-project.org mailing list
>> > https://stat.ethz.ch/mailman/listinfo/r-help
>> > PLEASE do read the posting guide
>>
http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html>
>> > and provide commented, minimal, self-contained, reproducible code.
>> >
>>
>
>
[[alternative HTML version deleted]]