Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: Using equation with natural logarithm in nls

Good day,

I am struggling with R and natural logarithm (ln). Firstly, I cannot find a ln(x) function in R. I have noticed that log(x) is the same as ln(x) (when using ln(x) with a calculator).

In R:

log(5) = 1.609438

And with a calculator:

ln(5) = 1.609438
log(5) = 0.69897

I'm trying to fit an equation in R (this is exactly how I found in the literature of 3 references):

y = a + b(x/305) + c(x/305)2 + d ln(305/x) + f ln2(305/x)

Is it correct to use the following syntax in R to use the equation?

y ~ a + b*(x/305) + c*((x/305)^2) + d*log(305/x) + f*(log(305/x))^2

The idea is to use this function with nls() in R. Thanks in advance!

like image 892
wernerfeuer Avatar asked Jun 19 '14 10:06

wernerfeuer


People also ask

How do you write natural log in R?

To calculate the natural log in R, use the log() function. The default setting of this function is to return the natural logarithm of a value. But through a package called SciViews, you can use the ln() function, which also calculates the natural log in R.

Is log the same as ln in R?

log in R means the natural logarithm. This is the convention of mathematicians, since "common" logarithms have no mathematical interest. The "ln" abbreviation is something that was introduced to make things less confusing to students.

Can you do ln in R?

R log Functionlog(x) function computes natural logarithms (Ln) for a number or vector x by default. If the base is specified, log(x,b) computes logarithms with base b.

What does ln mean in R?

ln: Logarithms. To avoid confusion using the default log() function, which is natural logarithm, but spells out like base 10 logarithm in the mind of some beginneRs, we define ln() and ln1p() as wrappers for log()`` with default base = exp(1) argument and for log1p() , respectively.


2 Answers

In R, log is the natural logarithm. In calculators, log usually means base 10 logarithm. To achieve that in R you can use the log10 function.

log(5)
## [1] 1.609438
log10
## [1] 0.69897(5)

As for your formula, it seems correct, since log is the natural logarithm.

like image 192
alko989 Avatar answered Oct 20 '22 22:10

alko989


In addition I will point out that your model

y ~ a + b*(x/305) + c*((x/305)^2) + d*log(305/x) + f*(log(305/x))^2

is linear in the statistical sense of being linear in the coefficients; it doesn't need to be linear in x.

You don't need nls to fit this model, you could use lm().

But remember to look at the I() function to express terms like (x/305)^2.

ETA example:

aDF <- data.frame(x=abs(rnorm(100)), y=rnorm(100))
lm(y ~ 1 + I(x/305) + I((x/305)^2) + log(305/x) + I(log(305/x)^2), data=aDF)
like image 4
user20637 Avatar answered Oct 20 '22 23:10

user20637