Plotting Modeled Data as my Regression Curve in R/RStudio - r

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")

Related

plot theoretic distribution against the real data histogram on one figure

I want to plot the histogram with real data and compare it with a theoretical normal distribution in one plot. But the scale looks different. Two plots have different scale
# you can generate some ramdom data on ystar which is realy data.
x<-seq(-4,4,length=200)
y<-dnorm(x,mean=0, sd=1)
plot(x,y, type = "l", lwd = 2, xlim = c(-3.5,3.5),ylim=c(0,0.7))
par(new = TRUE)
hist(ystar,xlim = c(-10,10),freq = FALSE,ylim=c(0,0.7),breaks = 50)
Desire output
Assuming that ystar is a vector, you should change this:
y<-dnorm(x,mean=0, sd=1)
To:
y<-dnorm(x,mean=mean(ystar), sd=sd(ystar))
This will produce a distribution function that approximately matches the histogram.
You should then be able to use the same x-limits for both the histogram and the theoretical distribution, which will eliminate the strange overlapping axis labels you have in your current version.

Understanding the Local Polynomial Regression

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

Smoothing using kernel and loess in R

I am trying to smooth my data set, using kernel or loess smoothing method. But, They are all not clear or not what I want. Several questions are the followings.
My x data is "conc" and y data is "depth", which is ex. cm.
1) Kernel smooth
k <- kernel("daniell", 150)
plot(k)
K <- kernapply(conc, k)
plot(conc~depth)
lines(K, col = "red")
Here, my data is smoothed by frequency=150. This means that every data point is averaged by neighboring (right and left) 150 data points? What "daniell" means? I could not find what it means online.
2) Loess smooth
p<-qplot(depth, conc, data=total)
p1 <- p + geom_smooth(method = "loess", size = 1, level=0.95)
Here, what is the default of loess smooth function? If I want to smooth my data with frequency=150 like above case (moving average by every 150 data point), how can I modify this code?
3) To show y-axis with log scale, I put "log10(conc)", instead of "conc", and it worked. But, I cannot change the y-axis tick label. I tried to use "scale_y_log10(limits = c(1,1e3))" in my code to show axis tick labe like 10^0, 10^1, 10^2..., but did not work.
Please answer my questions. Thanks a lot for your help.
Sum

Only plotting the fitted spline line and not the data points

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.

Trouble plotting predict line in R

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

Resources