Hi all,
I have a very quick question on how to use the summation sign in R for the
function.
Here?s a basic example: the function is sum(i=1 to 5)log(1-xi^2)
Id be grateful if someone knows how to do this without writing it out 5
times - I am looking sth along the lines of the following:
computeR <- function(x)
{
return (-sum(log(1-x^2))
}^
thank you vm in advance!
--
View this message in context: http://r.789695.n4.nabble.com/summation-sign-tp4647621.html
Sent from the R help mailing list archive at Nabble.com.
summation sign
3 messages · Peter Dalgaard, sffarooqi
On Oct 27, 2012, at 01:34 , sffarooqi wrote:
Hi all,
I have a very quick question on how to use the summation sign in R for the
function.
Here?s a basic example: the function is sum(i=1 to 5)log(1-xi^2)
Id be grateful if someone knows how to do this without writing it out 5
times - I am looking sth along the lines of the following:
computeR <- function(x)
{
return (-sum(log(1-x^2))
}^
thank you vm in advance!
You pretty much did it already:
x <- c(.5,.6,.7,.4,.8) -sum(log(1-x^2))
[1] 2.603318 I.e., just have x as a vector of length 5 and the rest is done automagically. (And, BTW, you don't need return() in a function like that in R, the return value is the value of the last expression.)
Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
thank you vm for the reply! Just to make things a little more complicated --does the equation hold even if the values of x are unknown? More specifically perhaps we can still refer to the made up example Sum(n equal 5)log(1-xi^2) In the problem I am working on, I am writing a modified Newton Optimisation code with linesearch. Therefore the values of x are not given but each x variable is some nx1 vector ..e.g. lets take a 3x1 vector. So referring to this example, where we had to sum 5 times, how could I write the function such that it sums 5 times, but each of the 5 x variables is taken to be a 3x1 vector. thank you very much again! -- View this message in context: http://r.789695.n4.nabble.com/summation-sign-tp4647621p4647655.html Sent from the R help mailing list archive at Nabble.com.