Hi, I have a function that generates a set of data but I am having problems determining the parameters using the nls fitting procedure. #### "MH"<-function(field,diameter,mu=10e-7,sig=0.1,Ms=100,chi=0){ #variables mu, sig, chi, Ms #input: field and diameter #all in CGS rho <- 5 kb <- 1.38e-16 t <- 300 length.d<-length(diameter) length.H<-length(field) M<-double(length.H) for (i in 1:length.H){ S1<-0 S2<-0 H <- field[i] for (j in 1:length.d){ d<-diameter[j] vol <- 4/3*pi*(d/2)^3 lognorm <- 1/(d*sig*sqrt(2*pi))*exp(-(log(d)-log(mu))^2/(2*sig^2)) lang <- 1/tanh(Ms*rho*vol*H/(kb*t))-1/(Ms*rho*vol*H/(kb*t)) S1 <- S1 + lognorm*vol*lang S2 <- S2 + lognorm*vol } M[i] <- Ms*S1/S2 + chi*H } M } ### I can calculate a set of data: htest<- (-10:10)*200 dtest<- (5:15)*1e-7 mtest<- MH(field=htest,diameter=dtest) ### However when I try to reverse engineer to calculate the parameters mu, sig, chi and Ms I run into problems. Could anyone shed some light on this problem? fit <- nls(M~MH(H,(5:15)*1e-7,mu,sig,Ms,chi),data=df,start=list(mu=10e-7, sig=0.1, chi=0, Ms=100)) ### Thanks, Paul -- View this message in context: http://r.789695.n4.nabble.com/Fitting-problem-tp2715234p2715234.html Sent from the R help mailing list archive at Nabble.com.
Hi r-help-bounces at r-project.org napsal dne 27.09.2010 13:13:28:> > Hi, > > I have a function that generates a set of data but I am having problems > determining the parameters using the nls fitting procedure. > > #### > > "MH"<-function(field,diameter,mu=10e-7,sig=0.1,Ms=100,chi=0){ > #variables mu, sig, chi, Ms > #input: field and diameter > #all in CGS > rho <- 5 > kb <- 1.38e-16 > t <- 300 > length.d<-length(diameter) > length.H<-length(field) > M<-double(length.H) > for (i in 1:length.H){ > S1<-0 > S2<-0 > H <- field[i] > for (j in 1:length.d){ > d<-diameter[j] > vol <- 4/3*pi*(d/2)^3 > lognorm <- 1/(d*sig*sqrt(2*pi))*exp(-(log(d)-log(mu))^2/(2*sig^2)) > lang <- 1/tanh(Ms*rho*vol*H/(kb*t))-1/(Ms*rho*vol*H/(kb*t)) > S1 <- S1 + lognorm*vol*lang > S2 <- S2 + lognorm*vol > } > M[i] <- Ms*S1/S2 + chi*H > } > M > } > > ### I can calculate a set of data: > > htest<- (-10:10)*200 > dtest<- (5:15)*1e-7 > mtest<- MH(field=htest,diameter=dtest) > > ### However when I try to reverse engineer to calculate the parametersmu,> sig, chi and Ms I run into problems. Could anyone shed some light onthis> problem?What problem? I get> fit <-nls(M~MH(H,(5:15)*1e-7,mu,sig,Ms,chi),data=df,start=list(mu=10e-7, + sig=0.1, chi=0, Ms=100)) Error in nls(M ~ MH(H, (5:15) * 1e-07, mu, sig, Ms, chi), data = df, start = list(mu = 1e-06, : 'data' must be a list or an environment evidently I have df only as function for density of F distribution. One comment. If you looked into help page you could find this warning Warning Do not use nls on artificial "zero-residual" data. Which I suppose you have. So add some noise to it. Regards Petr> > fit <-nls(M~MH(H,(5:15)*1e-7,mu,sig,Ms,chi),data=df,start=list(mu=10e-7,> sig=0.1, chi=0, Ms=100)) > > ### Thanks, Paul > -- > View this message in context:http://r.789695.n4.nabble.com/Fitting-problem-> tp2715234p2715234.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
Oops I forgot to add another line to the code (see below)!! You could add some noise if you want to. Generating the data from the function was merely a way to test if the fitting procedure works - I have experimental data that should allow me to calculate the parameters mu, sig, chi and Ms based upon my input M, H and diameter. df <- data.frame(H=htest,M=mtest) -- View this message in context: http://r.789695.n4.nabble.com/Fitting-problem-tp2715234p2715271.html Sent from the R help mailing list archive at Nabble.com.
r-help-bounces at r-project.org napsal dne 27.09.2010 13:36:51:> > Oops I forgot to add another line to the code (see below)!! You couldadd I do not have the previous code, I do not keep mails.> some noise if you want to. Generating the data from the function wasmerely You want to add some noise. nls ***does not work*** for artificial data without noise as stated in help page. Regards Petr> a way to test if the fitting procedure works - I have experimental datathat> should allow me to calculate the parameters mu, sig, chi and Ms basedupon> my input M, H and diameter. > > df <- data.frame(H=htest,M=mtest) > -- > View this message in context:http://r.789695.n4.nabble.com/Fitting-problem-> tp2715234p2715271.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
#### Updated! #### "MH"<-function(field,diameter,mu=10e-7,sig=0.1,Ms=100,chi=0){ #variables mu, sig, chi, Ms #input: field and diameter #all in CGS rho <- 5 kb <- 1.38e-16 t <- 300 length.d<-length(diameter) length.H<-length(field) M<-double(length.H) for (i in 1:length.H){ S1<-0 S2<-0 H <- field[i] for (j in 1:length.d){ d<-diameter[j] vol <- 4/3*pi*(d/2)^3 lognorm <- 1/(d*sig*sqrt(2*pi))*exp(-(log(d)-log(mu))^2/(2*sig^2)) lang <- 1/tanh(Ms*rho*vol*H/(kb*t))-1/(Ms*rho*vol*H/(kb*t)) S1 <- S1 + lognorm*vol*lang S2 <- S2 + lognorm*vol } M[i] <- Ms*S1/S2 + chi*H } M } ### htest<- (-10:10)*200 dtest<- (5:15)*1e-7 mtest<- MH(field=htest,diameter=dtest) + rnorm(21) ##also added some noise!! df <- data.frame(H=htest,M=mtest) fit <- nls(M~MH(H,(5:15)*1e-7,mu,sig,Ms,chi),data=df,start=list(mu=10e-7, sig=0.1, chi=0, Ms=100)) -- View this message in context: http://r.789695.n4.nabble.com/Fitting-problem-tp2715234p2715350.html Sent from the R help mailing list archive at Nabble.com.