[R] fitting a gaussian to some x,y data
Michael Koppelman
michael at isis.spa.umn.edu
Fri Aug 25 17:52:56 CEST 2006
I apologize if this is redundant. I've been Googling, searching the
archive and reading the help all morning and I am not getting closer
to my goal.
I have a series of data( xi, yi). It is not evenly sampled and it is
messy (meaning that there is a lot of scatter in the data). I want to
fit a normal distribution (i.e. a gaussian) to the data in order to
find the center. (The data has a loose "normal" look to it.) I have
done this with polynomial fitting in R with nls but want to try it
with a Gaussian (I am a student astronomer and have not a lot of
experience in statistics).
In looking at the fitdistr function, it seems to take as input a
bunch of x values and it will fit a gaussian to the histogram. That
is not what I need to do, I want to fit a normal distribution to the
x,y values and get out the parameters of the fit. I'm fooling with
nls but it seems to want the data in some other format or something
because it is complaining about "singular gradient".
I'm sure this isn't hard and the answer must be out there somewhere
but I can't find it. I appreciate any assistance.
Cheers,
Michael
filepath <- system.file("data", infile , package="datasets")
mm <-read.table(filepath)
mm
dmk <- data.frame( x=mm$V1, y=mm$V2)
attach(dmk)
plot(x,y)
#ymk <-nls(y~c*x^2+b*x+a,start=list(a=1,b=1,c=1),trace=TRUE)
ymk <-nls(y~a*exp(-x^2/sig),start=list(a=1,sig=1),trace=TRUE)
co = coef(ymk)
cmk <- list(a=co[[1]], b=co[[2]], c=co[[3]] )
More information about the R-help
mailing list