R exp() function - calculate exponent

R 语言实例R language examples

The R exp() function is used to calculate e raised to the power of x, that is, the natural exponential function.

exp() is the inverse operation of the natural logarithm log(), and is widely used in scenarios such as exponential growth models and logistic regression.

The syntax format of the exp() function is as follows:

exp(x)

Parameter description:

  • xInput a numerical value or a numerical vector.

example

Calculate the power of e.
print(exp(1))    # e^1 = e
print(exp(2))    # e^2
print(exp(0))    # e^0 = 1

Vector operations
x <- c(0, 1, 2, 3)
result.exp <- exp(x)
print(result.exp)

exp and log are inverse operations of each other.
print(exp(log(5)))   # e^(ln(5)) = 5
print(log(exp(5)))   # ln(e^5) = 5

Executing the above code outputs the following result:

[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 to probability:

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))
print(round(prob, 4))

Executing the above code outputs the following result:

[1] 0.1192 0.2689 0.5000 0.7311 0.8808

R 语言实例R language examples

Other extensions