username
username

Reputation: 1

How to plot 'log' and 'first difference of log' in R?

This might sound easy question to some people, but I am a beginner in R and I couldn't find an answer to my problem.

So, basically, I have uploaded my excel data into R. I have three variables. The first variable is in the format of the date (1994-01-03), the other two are just numbers.

dataset looks like this

I have plotted my main graphs using this command:

plot(dataset$observation_date,dataset$interest_rate,t="l", xlab = "Observation date" , ylab = "Interest Rate")

Now I need to plot a graph of the "log" and "first difference of log" of two variables.

Big thanks in advance for any help!!

Upvotes: 0

Views: 800

Answers (3)

Ghsh
Ghsh

Reputation: 1

You can use the following code for plotting a graph of the "log" of two variables:

x = log(dataset$observation_date)
y = log(dataset$interest_rate)
plot(x, y,
     type = "l", 
     xlab = "Observation date", 
     ylab = "Interest Rate")

You can use the following code for plotting a graph of the "first difference of log" of two variables:

x = diff(log(dataset$observation_date))
y = diff(log(dataset$interest_rate))
plot(x, y,
     type = "l", 
     xlab = "Observation date", 
     ylab = "Interest Rate")

Upvotes: 0

Bensstats
Bensstats

Reputation: 1056

If you want this all on one plot, you are going to need to use the lines() function and play around with the specifics such as the line types etc.

The code I have is:

# Creating some dummy data
dt<-data.frame(time=c(1:10),
               interest_rate = c(1:10))

dt$logged_interest<- log(dt$interest_rate)
# Make blank column
dt$logged_diff<- NA
# First difference excludes the first term
dt$logged_diff[2:10]<- diff(dt$logged_interest)

# The Plot
plot(dt$time,dt$interest_rate,
     type = "l", 
     xlab = "Time" , 
     ylab = "Interest Rate",
     ylim=c(-1,10),
     main="Interest Rates over Time")
lines(dt$time,dt$logged_interest,lty=2)
lines(dt$time,dt$logged_diff,lty=3)
legend("bottomright", 
       legend=c("Interest Rate", 
                "Logged Interest Rate",
                "First Difference of Log"),
       lty=1:3, cex=0.8)

enter image description here

Hope this is helpful!

Upvotes: 0

Rui Barradas
Rui Barradas

Reputation: 76450

1. Just the log.

From help("plot.default"):

log
a character string which contains "x" if the x axis is to be logarithmic, "y" if the y axis is to be logarithmic and "xy" or "yx" if both axes are to be logarithmic.

And here is a reproducible example.

plot(1:10, 1:10,
     type = "l", xlab = "Observation date" , ylab = "Interest Rate",
     log = "y")

Created on 2022-02-09 by the reprex package (v2.0.1)

2. log and diff log

To plot both the log and the first differences of the log, here are two ways.

plot(1:10, 1:10,
     type = "l", xlab = "Observation date" , ylab = "Interest Rate",
     xlim = c(0, 10), ylim = c(0, 10))
lines(1:10, log(1:10))
lines(2:10, diff(log(1:10)))

Created on 2022-02-09 by the reprex package (v2.0.1)

3. log and diff log (cont.)

The second way is more clear if an auxiliary function to compute the diff logs is defined.

dl <- function(x) c(0, diff(log(x)))
plot(1:10, 1:10,
     type = "l", xlab = "Observation date" , ylab = "Interest Rate",
     xlim = c(0, 10), ylim = c(0, 10))
curve(log, from = 1, to = 10, add = TRUE)
curve(dl, from = 1, to = 10, add = TRUE)

Created on 2022-02-09 by the reprex package (v2.0.1)

Upvotes: 3

Related Questions