[R] unsued argument

2012-08-11 Thread stephenxqy
It is a complex function, functions are quoted frequently, you may read from
bottom up
The independent variable for final fit is q


%%Rg0 is a function of L and b
Rg0sq<-function(L,b)L*b/6*(1-3/2*b/L+3/2*(b/L)^2-3/4*(b/L)^3*(1-exp(-2*L/b)))

%%alpha is a defined function
alpha<-function(x)(1+(x/3.12)^2+(x/8.67)^3)^(0.176/3)

%%w is a defined function
w<-function(x)(1+tanh(x-1.523)/0.1477)/2

%%It is Rg^2
Rgsq<-function(L,b)(alpha(L/b)*Rg0sq(L,b))

%%This is Rg
Rg<-function(L,b)(Rg0sq(L,b))^0.5

%%A debye function of q, the parameters are L and b
PDebye<-function(L,b)2*(exp(-q^2*Rg0sq(L,b))+q^2*Rg0sq(L,b)-1)/q^4/(Rg0sq(L,b))^2

%%Another function of q that quote w, PDebye, Rg, parameters are v, L and b
Pexv<-function(v,L,b)w(q*Rg(L,b))*PDebye(L,b)+(1-w(q*Rg(L,b)))(1.22*(q*Rg(L,b))^(-1/v)+0.4288*(q*Rg(L,b))^(-2/v)-1.651*(q*Rg(L,b))^(-1/v))

%%Another defined function
cf<-function(L,b,pa,pb)pa/(L/b)^pb

%%A function of q, quote Pexv, Rgsq, cf
Psfcex<-function(v,L,b,pa,pb)Pexv(v,L,b)+cf(L,b,pa,pb)*b/L/15*(4+7/q^2/Rgsq(L,b)-(11+7/q^2/Rgsq(L,b))*exp(-q^2*Rgsq(L,b)))

%%non-linear fit
Pfit<-function(p)sum((I-Psfcex(p[1],p[2],p[3],p[4],p[5]))^2)
wavefit<-nlm(Pfit,c(1.5,500,5,1,0.1),hessian=TRUE)

error code: Error in c(1.5, 500, 5, 1, 0.1) : unused argument(s) (0.1)
the fifth parameter is pb, which goes from Psfcex to alpha, actually L,b and
pa in alpha are used, why not pb?





--
View this message in context: 
http://r.789695.n4.nabble.com/unsued-argument-tp4640034.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


Re: [R] What's wrong with my code?

2012-08-12 Thread stephenxqy

Yes, missing * is the problem. Thank you a lot. Do you mean I need to 
incorporate all expression of Rg0sq, Rg etc. into the final Pfit function?

Qiuyang 

Date: Sat, 11 Aug 2012 21:18:03 -0700
From: ml-node+s789695n4640077...@n4.nabble.com
To: stephen...@hotmail.com
Subject: Re: What's wrong with my code?




stephenxqy wrote
It is a complex function, functions are quoted frequently, you may read from 
bottom up

The independent variable for final fit is q


%%Rg0 is a function of L and b

Rg0sq<-function(L,b)L*b/6*(1-3/2*b/L+3/2*(b/L)^2-3/4*(b/L)^3*(1-exp(-2*L/b)))


%%alpha is a defined function

alpha<-function(x)(1+(x/3.12)^2+(x/8.67)^3)^(0.176/3)


%%w is a defined function

w<-function(x)(1+tanh(x-1.523)/0.1477)/2


%%It is Rg^2

Rgsq<-function(L,b)(alpha(L/b)*Rg0sq(L,b))


%%This is Rg

Rg<-function(L,b)(Rg0sq(L,b))^0.5


%%A debye function of q, the parameters are L and b

PDebye<-function(L,b)2*(exp(-q^2*Rg0sq(L,b))+q^2*Rg0sq(L,b)-1)/q^4/(Rg0sq(L,b))^2


%%Another function of q that quote w, PDebye, Rg, parameters are v, L and b

Pexv<-function(v,L,b)w(q*Rg(L,b))*PDebye(L,b)+(1-w(q*Rg(L,b)))(1.22*(q*Rg(L,b))^(-1/v)+0.4288*(q*Rg(L,b))^(-2/v)-1.651*(q*Rg(L,b))^(-1/v))


%%Another defined function

cf<-function(L,b,pa,pb)pa/(L/b)^pb


%%A function of q, quote Pexv, Rgsq, cf

Psfcex<-function(v,L,b,pa,pb)Pexv(v,L,b)+cf(L,b,pa,pb)*b/L/15*(4+7/q^2/Rgsq(L,b)-(11+7/q^2/Rgsq(L,b))*exp(-q^2*Rgsq(L,b)))


%%non-linear fit

Pfit<-function(p)sum((I-Psfcex(p[1],p[2],p[3],p[4],p[5]))^2)

wavefit<-nlm(Pfit,c(1.5,500,5,1,0.1),hessian=TRUE)


error code: Error in c(1.5, 500, 5, 1, 0.1) : unused argument(s) (0.1)

the fifth parameter is pb, which goes from Psfcex to cf, actually pa which only 
exist in cf is used, why not pb?


1. What language is this? # starts a comment line in R not %%.

2. no data for q

3. what is I in Pfit?

4. Pexv: missing * or + or ... The expression in the function Pexv should 
probably read



w(q*Rg(L,b))*PDebye(L,b)+(1-w(q*Rg(L,b)))*(1.22*(q*Rg(L,b))^(-1/v)+0.4288*(q*Rg(L,b))^(-2/v)-1.651*(q*Rg(L,b))^(-1/v))
 


I have inserted  * after(1-w(q*Rg(L,b))) assuming that is what you want.


Your code is not reproducible since you have not provided data for q or I.

Generating some random data and assuming I is an identity matrix with 


set.seed(123)

q <- rnorm(20) + 5 

I <- diag(length(q))


adding wavefit at the end of the script and changing the %% to # your script 
runs with no errors but with warnings.

So I can't tell you why you  get the error message you got.


Finally: this is very inefficient code. There are repeated evaluations of same 
thing e.g. w(q*Rg(L,b)), Rg(L,b), Rg0sq(L,b) etc.


Berend













If you reply to this email, your message will be added to the 
discussion below:

http://r.789695.n4.nabble.com/What-s-wrong-with-my-code-tp4640034p4640077.html



To unsubscribe from What's wrong with my code?, click here.

NAML
  



--
View this message in context: 
http://r.789695.n4.nabble.com/What-s-wrong-with-my-code-tp4640034p4640086.html
Sent from the R help mailing list archive at Nabble.com.
[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] What's wrong with my code?

2012-08-12 Thread stephenxqy

Thanks!

Date: Sun, 12 Aug 2012 07:42:12 -0700
From: ml-node+s789695n4640093...@n4.nabble.com
To: stephen...@hotmail.com
Subject: RE: What's wrong with my code?




stephenxqy wrote
Yes, missing * is the problem. Thank you a lot. Do you mean I need to 
incorporate all expression of Rg0sq, Rg etc. into the final Pfit function?


Not necessarily. For example in function Pexv you evaluate Rg(L,b) 5 times.

You can also do this evaluating Rg(L,b) once and the same thing for the 
evaluation of w(...)


Pexv<-function(v,L,b) {

RgLb <- Rg(L,b)

wq <- w(q*RgLb) 


wq*PDebye(L,b)+(1-wq)*(1.22*(q*RgLb)^(-1/v)+0.4288*(q*RgLb)^(-2/v)-1.651*(q*RgLb)^(-1/v))
 

}


Other functions can be treated in a similar way.

Depending on how often Pfit is called and/or how long the vector q is, this 
could save you some cpu time.


Berend













If you reply to this email, your message will be added to the 
discussion below:

http://r.789695.n4.nabble.com/What-s-wrong-with-my-code-tp4640034p4640093.html



To unsubscribe from What's wrong with my code?, click here.

NAML
  



--
View this message in context: 
http://r.789695.n4.nabble.com/What-s-wrong-with-my-code-tp4640034p4640095.html
Sent from the R help mailing list archive at Nabble.com.
[[alternative HTML version deleted]]

__
R-help@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.