[R] Problem in log
Sarah Goslee
sarah.goslee at gmail.com
Wed Nov 30 00:58:04 CET 2011
On Tue, Nov 29, 2011 at 6:55 PM, Gyanendra Pokharel
<gyanendra.pokharel at gmail.com> wrote:
> yes, log of negative number is undefined and R also do the same and produces
> NaNs. Here I want to reject the value of exp(-alpha*d^(-beta)) when greater
> than 1, and want to run the loop otherwise.
> Thanks
Then just add another if() statement checking for that condition.
> On Tue, Nov 29, 2011 at 6:48 PM, Sarah Goslee <sarah.goslee at gmail.com>
> wrote:
>>
>> > 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.
>>
>> Wait... you're complaining that you can't take the natural log of a
>> negative
>> number in R?
>>
>> You can't do that anywhere. What do you expect to happen? The log of a
>> negative number IS NaN.
>>
>> Sarah
>> On Tue, Nov 29, 2011 at 6:28 PM, Gyanendra Pokharel
>> <gyanendra.pokharel at gmail.com> wrote:
>> > 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 at 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.
>> >>
>> >>
>>
>> --
>> Sarah Goslee
>> http://www.functionaldiversity.org
>
>
More information about the R-help
mailing list