Converting a plotted line into a model object - r

Let's say I have a series of points between which I want to plot straight lines:
x <- c(0, 2, 4, 7, 12)
y <- c(0, 0, 4, 5, 0)
plot(x, y, type = 'l')
How would I go about turning this plotted line into a simple model object? For instance, something with which I would be able to use the stats::predict() function to do something like this:
model.object <- ???
predict(model.object, data.frame(x = 3))
Output:
2
Or, at the very least, is there some way R can identify the slopes and intercepts of each of these lines between the points so I could manually create a piecewise function using if-statements?

While it's a bit different than predict, you can use approxfun to do interpolation between points
f <- approxfun(x, y)
f(3)
# [1] 2
Note that it just takes a vector of x values rather than a data.frame to make predictions.

Related

Surface of bivariate discontinuous function

I have a bivariate step function that I want to create a surface of. The function looks essentially as follows:
df<-data.frame(a = rnorm(100, 0, 10), b = rnorm(100, 0, 10))
f<-function(x,y){
mean(df$a * x >= df$b * y)
}
When I use plot3d of the rgl package, I always receive an error message like
Error in dim(zvals) <- dim(xvals) :
dims [product 10201] do not match the length of object [1]
What is the problem here? Is there any alternative how to 3d-plot my function?
The problem is the definition of f. plot3d(f) is going to pass in vectors for x and y, and your function is going to take the mean of everything and return a single value.
The simplest way to fix this is to call the Vectorize function, which wraps f in loops to compute it separately for each x, y pair. For example, with your definition of f as in the question,
plot3d(Vectorize(f), xlim = c(-2,2), ylim = c(-2, 2))
produces this plot:

R Statistics Distributions Plotting

I am having some trouble with a homework I have at Statistics.
I am required to graphical represent the density and the distribution function in two inline plots for a set of parameters at my choice ( there must be minimum 4 ) for Student, Fisher and ChiS repartitions.
Let's take only the example of Student Repartition.
From what I have searched on the internet, I have come with this:
First, I need to generate some random values.
x <- rnorm( 20, 0, 1 )
Question 1: I need to generate 4 of this?
Then I have to plot these values with:
plot(dt( x, df = 1))
plot(pt( x, df = 1))
But, how to do this for four set of parameters? They should be represented in the same plot.
Is this the good approach to what I came so far?
Please, tell me if I'm wrong.
To plot several densities of a certain distribution, you have to first have a support vector, in this case x below.
Then compute the values of the densities with the parameters of your choice.
Then plot them.
In the code that follows, I will plot 4 Sudent-t pdf's, with degrees of freedom 1 to 4.
x <- seq(-5, 5, by = 0.01) # The support vector
y <- sapply(1:4, function(d) dt(x, df = d))
# Open an empty plot first
plot(1, type = "n", xlim = c(-5, 5), ylim = c(0, 0.5))
for(i in 1:4){
lines(x, y[, i], col = i)
}
Then you can make the graph prettier, by adding a main title, changing the axis titles, etc.
If you want other distributions, such as the F or Chi-squared, you will use x strictly positive, for instance x <- seq(0.0001, 10, by = 0.01).

Drawing a line on time series plot

I have the next time series object, which I plot it using plot:
ts <- ts(c(1:4,2:5,3:6,4:7,5:8,6:9,7:10), frequency = 4)
plot(ts)
Plot of the ts
Now, I would like to draw a line between the first and the last observation using abline, but, although R doesn't show any error, it seems that it doesn't work when using it on a plot of time series. The code used to try to draw the line was:
abline(a = 1, b = (ts[length(ts)]- ts[1]) / (length(ts)-1))
Did anyone had the same problem and manage to solve it?
ts <- ts(c(1:4,2:5,3:6,4:7,5:8,6:9,7:10), frequency = 4)
plot(ts, xlim=c(1,8), type="o")
x1 <- 1; y1 <- ts[1]
x2<- 7.75; y2 <- ts[length(ts)]
abline(a = y1-x1*(y1-y2)/(x1-x2), b = (y1-y2)/(x1-x2) )
grid()
I am not sure that I fully understand what you want to achieve. But the following may be of help.
You may use the following simple code using lines:
# your code
ts <- ts(c(1:4,2:5,3:6,4:7,5:8,6:9,7:10), frequency = 4)
plot(ts)
# drawing a blue line from (1, 1) to (8, 10)
lines(x = c(1, 8), y = c(1,10), col="blue")
which yields the following simple plot

Using user-defined functions within "curve" function in R graphics

I am needing to produce normally distributed density plots with different total areas (summing to 1). Using the following function, I can specify the lambda - which gives the relative area:
sdnorm <- function(x, mean=0, sd=1, lambda=1){lambda*dnorm(x, mean=mean, sd=sd)}
I then want to plot up the function using different parameters. Using ggplot2, this code works:
require(ggplot2)
qplot(x, geom="blank") + stat_function(fun=sdnorm,args=list(mean=8,sd=2,lambda=0.7)) +
stat_function(fun=sdnorm,args=list(mean=18,sd=4,lambda=0.30))
but I really want to do this in base R graphics, for which I think I need to use the "curve" function. However, I am struggling to get this to work.
If you take a look at the help file for ? curve, you'll see that the first argument can be a number of different things:
The name of a function, or a call or an expression written as a function of x which will evaluate to an object of the same length as x.
This means you can specify the first argument as either a function name or an expression, so you could just do:
curve(sdnorm)
to get a plot of the function with its default arguments. Otherwise, to recreate your ggplot2 representation you would want to do:
curve(sdnorm(x, mean=8,sd=2,lambda=0.7), from = 0, to = 30)
curve(sdnorm(x, mean=18,sd=4,lambda=0.30), add = TRUE)
The result:
You can do the following in base R
x <- seq(0, 50, 1)
plot(x, sdnorm(x, mean = 8, sd = 2, lambda = 0.7), type = 'l', ylab = 'y')
lines(x, sdnorm(x, mean = 18, sd = 4, lambda = 0.30))
EDIT I added ylab = 'y' and updated the picture to have the y-axis re-labeled.
This should get you started.

How do I plot a power function 1-\phi(4.65-x/2) in R?

I want to plot a [power function][1] in R, namely 1-\phi(4.65-z/2). This can be written as \int_{-\infty}^{4.65-z/2}\frac{1}{2\pi} \exp(-\frac{x^2}{2}}) in latex.
Can someone explain how to plot this? Is there a specific command for the phi function?
This function, \Phi, is a cumulative distribution function of a standard normal random variable, and yes, there is a function for that in R: dnorm. Hence,
z <- seq(-2, 20, length = 1000)
plot(z, 1 - dnorm(4.65 - z / 2), type = 'l')
# or also just curve(1 - dnorm(4.65 - x / 2), -2, 20)

Resources