R exp() Function - Calculating Exponentials
The R exp() function is used to compute e raised to the power of x, that is, the natural exponential function.
exp() is the inverse of the natural logarithm log(), and is widely used in scenarios such as exponential growth models and logistic regression.
The syntax of the exp() function is as follows:
exp(x)
Parameter Description:
-
x Input number or numeric vector.
Example
Compute the power of e
(
exp
(
1
)
)
e^1 = e
(
exp
(
2
)
)
e^2
(
exp
(
0
)
)
e^0 = 1
Vector operations
x
<-
c
(
0
,
1
,
2
,
3
)
result.
exp
<-
exp
(
x
)
(
result.
exp
)
exp and log are inverse operations of each other
(
exp
(
log
(
5
)
)
)
e^(ln(5)) = 5
(
log
(
exp
(
5
)
)
)
ln(e^5) = 5
The output of the code above is:
[1] 2.718282
[1] 7.389056
[1] 1
[1] 1.000000 2.718282 7.389056 20.085537
[1] 5
[1] 5
exp() is often used to convert log-odds back into probabilities:
Example
log-odds in logistic regression
log_odds
<-
c
(
2
,
1
,
0
,
1
,
2
)
Convert log-odds to probability
Probability = exp(log_odds) / (1 + exp(log_odds))
prob
<-
exp
(
log_odds
)
/
(
1
exp
(
log_odds
)
)
(
round
(
prob,
4
)
)
The output of the code above is:
[1] 0.1192 0.2689 0.5000 0.7311 0.8808
R Language Examples