0

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!!

MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • Welcome to SO! Can you please share a [reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) snipped of your data by adding the output of `dput(dataset)` into your question? – Dan Adams Feb 09 '22 at 20:09
  • That said, you can calculate the log with `log(x)` or `log10(x)` depending on your needs, and the first difference perhaps with `diff(log(x))`. But with your actual data it will be easier to help you. – Dan Adams Feb 09 '22 at 20:11

3 Answers3

3

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)

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
0

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!

Bensstats
  • 988
  • 5
  • 17
0

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")
benson23
  • 16,369
  • 9
  • 19
  • 38
Ghsh
  • 1