[R] Odp: Fitting problem

Petr PIKAL petr.pikal at precheza.cz
Mon Sep 27 13:32:25 CEST 2010


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 parameters 
mu,
> sig, chi and Ms I run into problems.  Could anyone shed some light on 
this
> 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 guide 
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list