Mary
Mary

Reputation: 27

Hi I am new in r and trying to use grad in r

log1<-function(x,theta){ #make function with 2 argument
    return(-length(x)*log(2*pi)+sum(log(1-cos(x-theta))))
 }
g<-grad(func=log1, theta) # But I am getting an error. 
  1. I am getting an error when I use the gradient function.

Upvotes: 0

Views: 661

Answers (1)

Titus Rotich
Titus Rotich

Reputation: 88

your function log1 requires two arguments x and theta, but you're only passing theta, with no default for either arguments. Try:

library(numDeriv)
log1<-function(x,theta){ #make function with 2 argument
  return(-length(x)*log(2*pi)+sum(log(1-cos(x-theta))))
}
g<-grad(func = log1, x=4, theta=5)

or assign some default values for your variables so that you can do something like this:

log1<-function(x=3,theta=4){ #make function with 2 argument
  return(-length(x)*log(2*pi)+sum(log(1-cos(x-theta))))
}
g<-grad(func = log1, x=c(1,2,3))

Upvotes: 1

Related Questions