I have a question regarding plotting a exponential fit in Gnuplot and having the y-axis set in logscale. I am using Gnuplot 4.6.3.
I will present the few ways I have tried setting a range to fit in:
The function I want to fit on the data is f(x)=a+b*x
The first:
set log y
f(x)=a+b*x
fit [4:8] f(x) 'CT2A_OH_R_log.dat' using (log($1)):($2) via a,b
Resutls:
Read 15 points
Skipped 15 points outside range [x=4:8]
No data to fit
error during fit
The second:
fit f(x) 'CT2A_OH_R_log.dat' using (log($1)):($2) via a,b
Results:
Final set of parameters Asymptotic Standard Error
a =16.9858 +/- 6.299 (37.08%)
b =-8.43215 +/- 3.502 (41.53%)
gnuplot> plot 'CT2A_OH_R_log.dat', f(x) via a,b
Obviously the fit is ridiculous but this is the only way I know of that actually gives me a fit. I know the problem most likely lies within the defining of the xrange when fitting but what else can I write there to fix this?
Below I will show image of my plot (f(x) is not showing as it is plotted in the negative area.)
'cause a logscale of the y axis requires the logarithm of the y axis during the fit ;-)
fit [4:8] f(x) 'CT2A_OH_R_log.dat' u 1:(log($2)) via a,b
NOTE
If you want to use the logarithm based on 10, you have to use log($2)/log(10) or log10($2).
#Tom Solid
gnuplot> fit [4:8] [0.1:60] f(x) 'CT2A_OH_R_log.dat' using 1:(log($2))
via a,b
gnuplot> plot 'CT2A_OH_R_log.dat', f(x) via a,b
This time the fit is exponential like.
So, I will post the picture of what I am getting now:
Related
uppose that i have a poisson distribution with mean of 6 i would like to plot a probability mass function which includes an overlay of the approximating normal density.
This is what i have tried
plot( dpois( x=0:10, lambda=6 ))
this produces
which is wrong since it doesnt contain an overlay of approxiamating noral density
How do i go about this?
Something like what you seem to be asking for (I'm outlining the commands and the basic ideas, but checking the help on the functions and trying should fill in the remaining details):
taking a wider range of x-values (out to at least 13 or so) and use xlim to extend the plot slightly into the negatives (maybe to -1.5) and
plotting the pmf of the Poisson with solid dots (similar to your command but with pch=16 as an argument to plot) with a suitable color, then
call points with the same x and y arguments as above and have type=h and lty=3 to get vertical dotted lines (to give a clear impression of the relative heights, somewhat akin to the appearance of a Cleveland dot-chart); I'd use the same colour as the dots or a slightly lighter/greyer version of the dot-colour
use curve to draw the normal curve with the same mean and standard deviation as the Poisson with mean 6 (see details at the Wikipedia page for the Poisson which gives the mean and variance), but across the wider range we plotted; I'd use a slightly contrasting colour for that.
I'd draw a light x-axis in (e.g. using abline with the h argument)
Putting all those suggestions together:
(However, while it's what you're asking for it's not strictly a suitable way to compare discrete and continuous variables since density and pmf are not on the same scale, since density is not probability -- the "right" comparison between a Poisson and an approximating normal would be on the scale of the cdfs so you compare like with like -- they'd both be on the scale of probabilities then)
There are a number of questions in this forum on locating intersections between a fitted model and some raw data. However, in my case, I am in an early stage project where I am still evaluating data.
To begin with, I have created a data frame that contains a ratio value whose ideal value should be 1.0. I have plotted the data frame and also used abline() function to plot a horizontal line at y=1.0. This horizontal line and the plot of ratios intersect at some point.
plot(a$TIME.STAMP, a$PROCESS.RATIO,
xlab='Time (5s)',
ylab='Process ratio',
col='darkolivegreen',
type='l')
abline(h=1.0,col='red')
My aim is to locate the intersection point, say x and draw two vertical lines at x±k, as abline(v=x-k) and abline(v=x+k) where, k is certain band of tolerance.
Applying a grid on the plot is not really an option because this plot will be a part of a multi-panel plot. And, because ratio data is very tightly laid out, the plot will not be too readable. Finally, the x±k will be quite valuable in my discussions with the domain experts.
Can you please guide me how to achieve this?
Here are two solutions. The first one uses locator() and will be useful if you do not have too many charts to produce:
x <- 1:5
y <- log(1:5)
df1 <-data.frame(x= 1:5,y=log(1:5))
k <-0.5
plot(df1,type="o",lwd=2)
abline(h=1, col="red")
locator()
By clicking on the intersection (and stopping the locator top left of the chart), you will get the intersection:
> locator()
$x
[1] 2.765327
$y
[1] 1.002495
You would then add abline(v=2.765327).
If you need a more programmable way of finding the intersection, we will have to estimate the function of your data. Unfortunately, you haven’t provided us with PROCESS.RATIO, so we can only guess what your data looks like. Hopefully, the data is smooth. Here’s a solution that should work with nonlinear data. As you can see in the previous chart, all R does is draw a line between the dots. So, we have to fit a curve in there. Here I’m fitting the data with a polynomial of order 2. If your data is less linear, you can try increasing the order (2 here). If your data is linear, use a simple lm.
fit <-lm(y~poly(x,2))
newx <-data.frame(x=seq(0,5,0.01))
fitline = predict(fit, newdata=newx)
est <-data.frame(newx,fitline)
plot(df1,type="o",lwd=2)
abline(h=1, col="red")
lines(est, col="blue",lwd=2)
Using this fitted curve, we can then find the closest point to y=1. Once we have that point, we can draw vertical lines at the intersection and at +/-k.
cross <-est[which.min(abs(1-est$fitline)),] #find closest to 1
plot(df1,type="o",lwd=2)
abline(h=1)
abline(v=cross[1], col="green")
abline(v=cross[1]-k, col="purple")
abline(v=cross[1]+k, col="purple")
(i know it must be incredibely easy, but i'm strugling with it in R:)
i have dataset of x and y values saved in X and Y vectors. I know that plot of the data should follow exactly -45 degree line (see image below)
How do i find such -45 degree line that best fits the data (+ all these statistics available from summary(lm(...))? I've tried lm, but i can't force it to abandon fitting the slope parameter
Thank you
After trying: lm(y~1,offset=-x) and applying abline(coefficient, -1) i obtain following plot (see below)
black line is abline plot, yellow one is mine guess of fit -- what's wrong with lm or do i miss totally something?
I believe the solution from #BenBolker is correct and perhaps you are using the wrong coefficient:
lm1 <- lm(y~1,offset=-x,data=df)
plot(df)
abline(coefficients(lm1),-1)
This produces:
This fit looks like the correct fit to me. The intercept is -2.217.
Since you state that:
y = -1*x + b
then
y+x = b
So calculate the mean of (y+x) and you get the average value of b
mean(y+x)
I would like to create a Student's t distribution density plot with a mean of 0.02 instead of 0. is that possible to do?
the distribtion should have 2 degrees of freedom.
is this possible to do?
I tried the following:
X<-rnorm(100000,mean=0.02, sd=(1/sqrt(878)))
pop.mean<-mean(X)
t<-sapply(1:10000, function(x) (mean(sample(X,100))-pop.mean)/(1/sqrt(878)))
plot(density(t))
Is this approach correct?
If it is correct, how can I get the real densities, not just the approximation?
Your statement and example contradict each other somewhat.
Do you want a non-central t distribution which is based on a normal with mean 0.02? This is what your example suggests, but note that the non-central t is not just a shifted t, it is now skewed.
If you want the non-central t then you can plot it with a command like:
curve(dt(x,2,0.02), from=-5, to=6)
Or, do you want a shifted t distribution? A distribution that is symmetric around 0.02 with the shape of a t distribution?
You can plot the curve shifted by using a command like:
curve(dt(x-0.02,2), from=-5, to=6 )
The curve function has an add argument that you could use to plot both on the same plot if you want to compare them (not much difference in this case), changing the color on one of them would be suggested.
I need to plot a function f(x), where x is discrete set of values (in my case positive integers). I couldn't find a way to specify a step-size when using the range option and samples doesn't seem to be the right solution. Finally, I would like to approximate f(x) with a smooth function.
I don't quite understand why samples is not the solution to your problem.
If I want to plot sin(x) on an interval between 0 and 10 with a point at every integer I use
set xrange [0:10]
set sample 11
plot sin(x) w p
Obviously the number of samples is xmax-xmin+1 (10 - 0 + 1 = 11).
Finally to tackle the approximation problem have a look at this website which discusses linear least squares fitting. For simple linear interpolation use lp instead of p.
Or alternatively, play around with the ceil(x) or floor(x) functions.
Maybe have a look at this example:
http://gnuplot.sourceforge.net/demo/prob2.html
You can do:
plot [1:12] '+' u ($0):(f($0))
Where, $0 will be replaced by 1, 2, ..., 12. You can even do a smooth on this. For instance:
f(x)=sin(2*x)
plot [1:12] f(x) t 'the function'\
, '+' u ($0):(f($0)) t 'the points'\
, '+' u ($0):(f($0)) smooth cspline t 'the smooth'