I have checked my references, it seems to me that to fit a dataset with x and y, many tutorial need to first plot the x and y, then the fitted line is plot. The normal procedure is like below:
## Calculate the fitted line
smoothingSpline = smooth.spline(tree_number[2:100], jaccard[1:99], spar=0.35)
plot(tree_number[2:100],jaccard[1:99]) #plot the data points
lines(smoothingSpline) # add the fitted spline line.
However, I do not want to plot the tree_number and jaccard, but rather, I only want to plot the fitted spline line in the plot, how should I do?
You can use the associcated plot function:
plot(smoothingSpline, type="l")
Or you can extract the x and y values explicitly and plot them
plot(smoothingSpline$x, smoothingSpline$y, type="l")
Why not just plot(smoothingSpline, type = "l")? That should allow you to add the fitted spline line without having to first plot the data points.
Related
Could someone explain me why I get different lines when I plot? Somehow I thought the line should be the same
data(aircraft)
help(aircraft)
attach(aircraft)
lgWeight <- log(Weight)
library(KernSmooth)
# a) Fit a nonparametric regression to data (xi,yi) and save the estimated values mˆ (xi).
# Regression of degree 2 polynomial of lgWeight against Yr
op <- par(mfrow=c(2,1))
lpr1 <- locpoly(Yr,lgWeight, bandwidth=7, degree = 2, gridsize = length(Yr))
plot(Yr,lgWeight,col="grey", ylab="Log(Weight)", xlab = "Year")
lines(lpr1,lwd=2, col="blue")
lines(lpr1$y, col="black")
How can I get the values from the model? If I print the model, it gives me the values on $x and $y, but somehow if I plot them, is not the same as the blue line. I need the values of the fitted model (blue) for every x, could someone help me?
The fitted model (blue curve) is correctly in lpr1. As you said, the correct y-values are in lpr1$y and the correct x-values are in lpr1$x.
The reason the second plot looks like a straight line is because you are only giving the plot function one variable, lpr1$y. Since you don't specify the x-coordinates, R will automatically plot them along an index, from 1 to the length of the y variable.
The following are two explicit and equivalent ways to plot the curve and line:
lines(x = lpr1$x, y = lpr1$y,lwd=2, col="blue") # plots curve
lines(x = 1:length(lpr1$y), y = lpr1$y, col="black") # plot line
I have two numerical data sets; one experimental, and one modeled. I have plotted the data set as a scatter plot in R. The experimental data are:
x=c(1.11,2.84,3.97,6.40,7.60,7.43,5.75,3.60,3.59)
y=c(0.973,0.818,0.74,0.44,0.2688,0.282,0.50,0.613,0.656)
This is a pretty straight forward scatter plot. I also have some modeled data I acquired using an equation. This data needs to be superimposed on this plot. The modeled data is as follows:
x = c(1,2,3,4,5,6,7,8,9,10,11,12)
y = c(.994,.954,.860,.721,.570,.434,.362,.244,.185,.142,.110,.087)
How do I take these data, make a regression curve with them, and use them to generate some simple statistics comparing the modeled data to the experimental data?
Thanks!
First plot line
x = c(1,2,3,4,5,6,7,8,9,10,11,12)
y = c(.994,.954,.860,.721,.570,.434,.362,.244,.185,.142,.110,.087)
plot(x, y, type = "l")
Then points
x=c(1.11,2.84,3.97,6.40,7.60,7.43,5.75,3.60,3.59)
y=c(0.973,0.818,0.74,0.44,0.2688,0.282,0.50,0.613,0.656)
points(x, y, col = "red")
I have fit a LOESS local regression to some data and I want to be able to find the X value associated with a given Y value.
plot(cars, main = "Stopping Distance versus Speed")
car_loess <- loess(cars$dist~cars$speed,span=.5)
lines(1:50, predict(car_loess,data.frame(speed=1:50)))
I was hoping that I could use teh inverse.predict function from the chemCal package, but that does not work for LOESS objects.
Does anyone have any idea how I might be able to do this calibrationa in a better way than predicticting Y values from a long vector of X values and looking through the resulting fitted Y for the Y value of interest and taking its corresponding X value?
Practically speaking in the above example, let's say I wanted to find the speed at which the stopping distance is 15.
Thanks!
The predicted line that you added to the plot is not quite right. Use code like this instead:
# plot the loess line
lines(cars$speed, car_loess$fitted, col="red")
You can use the approx() function to get a linear approximation from the loess line at a give y value. It works just fine for the example that you give:
# define a given y value at which you wish to approximate x from the loess line
givenY <- 15
estX <- approx(x=car_loess$fitted, y=car_loess$x, xout=givenY)$y
# add corresponding lines to the plot
abline(h=givenY, lty=2)
abline(v=estX, lty=2)
But, with a loess fit, there may be more than one x for a given y. The approach I am suggesting does not provide you with ALL of the x values for the given y. For example ...
# example with non-monotonic x-y relation
y <- c(1:20, 19:1, 2:20)
x <- seq(y)
plot(x, y)
fit <- loess(y ~ x)
# plot the loess line
lines(x, fit$fitted, col="red")
# define a given y value at which you wish to approximate x from the loess line
givenY <- 15
estX <- approx(x=fit$fitted, y=fit$x, xout=givenY)$y
# add corresponding lines to the plot
abline(h=givenY, lty=2)
abline(v=estX, lty=2)
I have a simple data set with two columns of data- K and SwStr.
K = c(.259, .215, .224, .223, .262, .233)
SwStr = c(.130, .117, .117, .114, .113, .111)
I plotted the data using:
plot(res$K, res$SwStr)
I want to plot the result of a linear model, using SwStr to predict K. I try to do that using:
graphic<-lm(K~SwStr-1, data=res)
P=predict(graphic)
plot(res$K, res$SwStr)
lines(P, lty="dashed", col="green", lwd=3)
But when I do this, I don't get any line plotted. What am I doing wrong?
(1) You are inverting the axes of the original plot. If you want SwStr on the x axis and K on the y axis you need
plot(res$SwStr, res$K)
or
with(res,plot(K~SwStr))
If you check the actual values of the plotted points on the graph, this might be obvious (especially if K and SwStr have different magnitudes) ...
For lm fits you can also use abline(graphic,...)
edit: (2) You also have to realize that predict gives just the predicted y values, not the x values. So you want something like this:
K=c(.259, .215, .224, .223, .262, .233)
SwStr=c(.130, .117, .117, .114, .113, .111)
g <- lm(K~SwStr-1)
par(las=1,bty="l") ## my favourites
plot(K~SwStr)
P <- predict(g)
lines(SwStr,P)
Depending on the situation, you may also want to use the newdata argument to predict to specify a set of evenly spaced x values ...
How to plot the density of a single column dataset as dots? For example
x <- c(1:40)
On the same plot using the same scale of the x-axis and y-axis, how to add another data set as line format which represent the density of another data that represents the equation of
y = exp(-x)
to the plot?
The equation is corrected to be y = exp(-x).
So, by doing plot(density(x)) or plot(density(y)), I got two separated figures. How to add them in the same axis and using dots for x, smoothed line for y?
You can add a line to a plot with the lines() function. Your code, modified to do what you asked for, is the following:
x <- 1:40
y <- exp(-x)
plot(density(x), type = "p")
lines(density(y))
Note that we specified the plot to give us points with the type parameter and then added the density curve for y with lines. The help pages for ?plot, ?par, ?lines would be some insightful reading. Also, check out the R Graph Gallery to view some more sophisticated graphs that generally have the source code attached to them.