Re: [R] How to do a backward calculation for each record in a dataset

2013-02-19 Thread Prakasit Singkateera
Hi everyone,

From your helps of giving me several ideas, eventually I can solve the
posted problem. Here is the R code. It can be done by applying the
uniroot.all to the data frame together with the proper form of equation
(slightly modification of the original equation).

#Generate the sample data frame
customer.name = c(John,Mike,Peter)
product = c(Toothpaste,Toothpaste,Toothpaste)
cost = c(30,45,40)
mydata = data.frame(customer.name,product,cost)

#Original cost function - not used
#fcost = function(orders) 3.40 + (1.20 * orders^2)

#Slightly modification of the cost function to be a proper form for root
finding
#This is basically to set == 3.40 + (1.20 * orders^2) - fcost = 0
f.to.findroot = function(orders,fcost) 3.40 + (1.20 * orders^2) - fcost

#Using rootSolve package which contains uniroot.all function
library(rootSolve)

#Using plyr package which contains adply function
library(plyr)

#Use uniroot function to find the 'orders' variable (from the f.to.findroot
function) for each customer and put it into no.of.orders column in
mysolution data frame
#Replace 'fcost' with 'cost' column from mydata
#Interval of 0 to 1,000 is to make the f.to.findroot function have both
negative and positive sign, otherwise uniroot.all will give an error
mysolution = data.frame(adply(mydata, 1, summarize, no.of.orders =
uniroot.all(f.to.findroot,interval = c(0,1000),fcost=cost)))
mysolution

#Remove the redundant mydata as mysolution it is an extended version of
mydata
rm(mydata)

#Note uniroot.all can be used for both linear (e.g.orders^1) and non-linear
(e.g.orders^2) equations.


Thank you,
Prakasit Singkateera

[[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] How to do a backward calculation for each record in a dataset

2013-02-19 Thread Berend Hasselman

On 19-02-2013, at 09:55, Prakasit Singkateera asltjoey.rs...@gmail.com wrote:

 Hi everyone,
 
 From your helps of giving me several ideas, eventually I can solve the posted 
 problem. Here is the R code. It can be done by applying the uniroot.all to 
 the data frame together with the proper form of equation (slightly 
 modification of the original equation).
 
 #Generate the sample data frame
 customer.name = c(John,Mike,Peter)
 product = c(Toothpaste,Toothpaste,Toothpaste)
 cost = c(30,45,40)
 mydata = data.frame(customer.name,product,cost)
 
 #Original cost function - not used
 #fcost = function(orders) 3.40 + (1.20 * orders^2)
 
 #Slightly modification of the cost function to be a proper form for root 
 finding
 #This is basically to set == 3.40 + (1.20 * orders^2) - fcost = 0
 f.to.findroot = function(orders,fcost) 3.40 + (1.20 * orders^2) - fcost
 
 #Using rootSolve package which contains uniroot.all function
 library(rootSolve)
 
 #Using plyr package which contains adply function
 library(plyr)
 
 #Use uniroot function to find the 'orders' variable (from the f.to.findroot 
 function) for each customer and put it into no.of.orders column in mysolution 
 data frame
 #Replace 'fcost' with 'cost' column from mydata
 #Interval of 0 to 1,000 is to make the f.to.findroot function have both 
 negative and positive sign, otherwise uniroot.all will give an error
 mysolution = data.frame(adply(mydata, 1, summarize, no.of.orders = 
 uniroot.all(f.to.findroot,interval = c(0,1000),fcost=cost)))
 mysolution
 
 #Remove the redundant mydata as mysolution it is an extended version of mydata
 rm(mydata)
 
 #Note uniroot.all can be used for both linear (e.g.orders^1) and non-linear 
 (e.g.orders^2) equations.


1. You don't need rootSolve. uniroot is sufficient in your case. You don't have 
multiple roots for each element of cost.

2. You are now storing more information than you require into the resulting 
dataframe. Use uniroot(…)$root to store only the root of the equation.

3. you don't need plyr. You can do it like this

mysolution - within(mydata, 
no.of.orders - sapply(seq_len(length(cost)),function(k) 
uniroot(f.to.findroot,interval = c(0,1000),fcost=cost[k])$root )
)
# for printing the dataframe

mysolution

Berend

__
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] How to do a backward calculation for each record in a dataset

2013-02-19 Thread Prakasit Singkateera
Hi Berend,

Your method is really much better. Thank you very much. (Yes I also forgot
to add the $root at the end.)

Best,
Prakasit


On Tue, Feb 19, 2013 at 10:51 PM, Berend Hasselman b...@xs4all.nl wrote:


 On 19-02-2013, at 09:55, Prakasit Singkateera asltjoey.rs...@gmail.com
 wrote:

  Hi everyone,
 
  From your helps of giving me several ideas, eventually I can solve the
 posted problem. Here is the R code. It can be done by applying the
 uniroot.all to the data frame together with the proper form of equation
 (slightly modification of the original equation).
 
  #Generate the sample data frame
  customer.name = c(John,Mike,Peter)
  product = c(Toothpaste,Toothpaste,Toothpaste)
  cost = c(30,45,40)
  mydata = data.frame(customer.name,product,cost)
 
  #Original cost function - not used
  #fcost = function(orders) 3.40 + (1.20 * orders^2)
 
  #Slightly modification of the cost function to be a proper form for root
 finding
  #This is basically to set == 3.40 + (1.20 * orders^2) - fcost = 0
  f.to.findroot = function(orders,fcost) 3.40 + (1.20 * orders^2) - fcost
 
  #Using rootSolve package which contains uniroot.all function
  library(rootSolve)
 
  #Using plyr package which contains adply function
  library(plyr)
 
  #Use uniroot function to find the 'orders' variable (from the
 f.to.findroot function) for each customer and put it into no.of.orders
 column in mysolution data frame
  #Replace 'fcost' with 'cost' column from mydata
  #Interval of 0 to 1,000 is to make the f.to.findroot function have both
 negative and positive sign, otherwise uniroot.all will give an error
  mysolution = data.frame(adply(mydata, 1, summarize, no.of.orders =
 uniroot.all(f.to.findroot,interval = c(0,1000),fcost=cost)))
  mysolution
 
  #Remove the redundant mydata as mysolution it is an extended version of
 mydata
  rm(mydata)
 
  #Note uniroot.all can be used for both linear (e.g.orders^1) and
 non-linear (e.g.orders^2) equations.


 1. You don't need rootSolve. uniroot is sufficient in your case. You don't
 have multiple roots for each element of cost.

 2. You are now storing more information than you require into the
 resulting dataframe. Use uniroot(…)$root to store only the root of the
 equation.

 3. you don't need plyr. You can do it like this

 mysolution - within(mydata,
 no.of.orders - sapply(seq_len(length(cost)),function(k)
 uniroot(f.to.findroot,interval = c(0,1000),fcost=cost[k])$root )
 )
 # for printing the dataframe

 mysolution

 Berend



[[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] How to do a backward calculation for each record in a dataset

2013-02-18 Thread Prakasit Singkateera
Hi all,

Firstly, it is not a homework. I am working for a hotel booking company in
Thailand but I don't want to explain a complex equation and concept here so
I keep it simple and closely related to what I am trying to solve.I apology
if my question is not clear enough.

I am new to R and previously this problem can be solved easily in Excel
using the Goal Seek tool. An example related to my question is when we
use the PMT formula (in Excel) to find the loan payment amount for the
given values of parameters i.e. interest rate, total number of payments,
and principal amount of the loan.

loan_payment_amt_of_each_period =
PMT(interest_rate,total_number_of_payments,principal_amt)

The question is when you know exactly on a monthly basis that you can
afford only X amount of money to pay and you want to know how many months
you have to do the payment given your monthly affordable money, the fixed
interest rate, and the principal amount of loan. Using Goal Seek tool in
Excel, it is like a backward solving for X given Y by not having to
transform anything from the original equation. Simply put the
loan_payment_amt_of_each_period you want and let the software calculate the
total_number_of_payments for you.

Thanks arun. But that was you solved the original equation and put it as a
new formula to R to calculate the result which is easy as long as the
original equation is not complex.


Thanks you,
Prakasit Singkateera




On Mon, Feb 18, 2013 at 1:18 AM, Bert Gunter gunter.ber...@gene.com wrote:

 Homework? We don't do homework here.

 -- Bert

 On Sun, Feb 17, 2013 at 5:10 AM, Prakasit Singkateera
 asltjoey.rs...@gmail.com wrote:
  Hi Experts,
 
  I have a dataset of 3 columns:
 
  customer.name product cost
  John Toothpaste 30
  Mike Toothpaste 45
  Peter Toothpaste 40
 
  And I have a function of cost whereby
 
  cost = 3.40 + (1.20 * no.of.orders^2)
 
  I want to do a backward calculation for each records (each customer) to
  find his no.of.orders and create a new column named no.of.orders in
 that
  dataset but I don't know how to do.
 
  Please help me.
 
  Thank you everyone,
  Prakasit
 
  [[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.



 --

 Bert Gunter
 Genentech Nonclinical Biostatistics

 Internal Contact Info:
 Phone: 467-7374
 Website:

 http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm


[[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] How to do a backward calculation for each record in a dataset

2013-02-18 Thread Berend Hasselman

On 18-02-2013, at 10:34, Prakasit Singkateera asltjoey.rs...@gmail.com wrote:

 Hi all,
 
 Firstly, it is not a homework. I am working for a hotel booking company in
 Thailand but I don't want to explain a complex equation and concept here so
 I keep it simple and closely related to what I am trying to solve.I apology
 if my question is not clear enough.
 
 I am new to R and previously this problem can be solved easily in Excel
 using the Goal Seek tool. An example related to my question is when we
 use the PMT formula (in Excel) to find the loan payment amount for the
 given values of parameters i.e. interest rate, total number of payments,
 and principal amount of the loan.
 
 loan_payment_amt_of_each_period =
 PMT(interest_rate,total_number_of_payments,principal_amt)
 
 The question is when you know exactly on a monthly basis that you can
 afford only X amount of money to pay and you want to know how many months
 you have to do the payment given your monthly affordable money, the fixed
 interest rate, and the principal amount of loan. Using Goal Seek tool in
 Excel, it is like a backward solving for X given Y by not having to
 transform anything from the original equation. Simply put the
 loan_payment_amt_of_each_period you want and let the software calculate the
 total_number_of_payments for you.
 

You can find R versions of the Excel functions here: 
http://factbased.blogspot.nl/2013/02/some-of-excel-finance-functions-in-r.html
The R code is here: http://pastebin.com/q7tyiEmM
I do not know if these are a correct translation of what's in Excel or Calc.

For you application to find the number of payments, you can use the R function 
uniroot for solving a single equation with one unknown.
Small example

# R version of Excel PMT function (as in LibreOffice Calc)
pmt - function(rate, nper, pv, fv=0, type=0) {
  rr - 1/(1+rate)^nper
  res - (-pv-fv*rr)*rate/(1-rr)
  return(res/(1+rate*type))
}

# here x is the number of payments
Rpmt - function(x, xrate, xpmt, xpv) xpmt - pmt(xrate,x,xpv)

irate - .05
xpmt - -10
xpv - 100

# testing
pmt(irate,10,xpv)
pmt(irate,5,xpv)
pmt(irate,20,xpv)
Rpmt(xpv/xpmt/2,xrate=irate, xpmt=xpmt, xpv=xpv)
Rpmt(xpv/xpmt*2,xrate=irate, xpmt=xpmt, xpv=xpv)

# find number of payments
z - uniroot(Rpmt,lower=1,upper=100, xrate=irate, xpmt=xpmt, xpv=xpv)  
z
#number of payments 
z$root

# check
Rpmt(z$root,xrate=irate, xpmt=xpmt, xpv=xpv)
pmt(irate,z$root,xpv)
 
Should you desire higher accuracy of the solution, use tol=1e-8 in the 
uniroot() call.

Berend

__
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] How to do a backward calculation for each record in a dataset

2013-02-18 Thread Rolf Turner


Some (quite a few!) years ago I wrote myself a wee function called
compInt() (compound interest) to do --- I think --- just what you require.
I have attached the code for this function and a help file for it.

If anyone else wants this code, and if the attachments don't get through 
the list,

let me know and I can send the stuff to you directly.

cheers,

Rolf Turner

On 02/18/2013 10:34 PM, Prakasit Singkateera wrote:

Hi all,

Firstly, it is not a homework. I am working for a hotel booking company in
Thailand but I don't want to explain a complex equation and concept here so
I keep it simple and closely related to what I am trying to solve.I apology
if my question is not clear enough.

I am new to R and previously this problem can be solved easily in Excel
using the Goal Seek tool. An example related to my question is when we
use the PMT formula (in Excel) to find the loan payment amount for the
given values of parameters i.e. interest rate, total number of payments,
and principal amount of the loan.

loan_payment_amt_of_each_period =
PMT(interest_rate,total_number_of_payments,principal_amt)

The question is when you know exactly on a monthly basis that you can
afford only X amount of money to pay and you want to know how many months
you have to do the payment given your monthly affordable money, the fixed
interest rate, and the principal amount of loan. Using Goal Seek tool in
Excel, it is like a backward solving for X given Y by not having to
transform anything from the original equation. Simply put the
loan_payment_amt_of_each_period you want and let the software calculate the
total_number_of_payments for you.

Thanks arun. But that was you solved the original equation and put it as a
new formula to R to calculate the result which is easy as long as the
original equation is not complex.


Thanks you,
Prakasit Singkateera


On Mon, Feb 18, 2013 at 1:18 AM, Bert Gunter gunter.ber...@gene.com wrote:


Homework? We don't do homework here.

-- Bert

On Sun, Feb 17, 2013 at 5:10 AM, Prakasit Singkateera
asltjoey.rs...@gmail.com wrote:

Hi Experts,

I have a dataset of 3 columns:

customer.name product cost
John Toothpaste 30
Mike Toothpaste 45
Peter Toothpaste 40

And I have a function of cost whereby

cost = 3.40 + (1.20 * no.of.orders^2)

I want to do a backward calculation for each records (each customer) to
find his no.of.orders and create a new column named no.of.orders in

that

dataset but I don't know how to do.

Please help me.

Thank you everyone,
Prakasit



--

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:

http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
compInt - function(P=NULL,r=NULL,n=NULL,a=NULL) {
#
# Function compInt.  To calculate one of the parameters P,r,n,a,
# associated with the compound interest formula,
#
#
# 12a
#  (1 + r/12)^n = 
#  (12a - rP)
#
# given the other three.
# P = principle, r = annual interest rate (compounded monthly),
# n = number of months until loan is paid off; a = monthly payment.
#

chk - sum(c(is.null(P),is.null(r),is.null(n),is.null(a)))
if(chk  1) stop(Must specify either ONE or ZERO non-null arguments.\n)

if(!is.null(P)  (!is.numeric(P) || length(P) != 1 || P = 0))
stop(Argument \P\ must be a positive numeric scalar.\n)
if(!is.null(r)  (!is.numeric(r) || length(r) != 1 || r = 0))
stop(Argument \r\ must be a positive numeric scalar.\n)
if(!is.null(n)  (!is.numeric(n) || length(n) != 1 || n = 0 ||
   !isTRUE(all.equal(n,round(n)
stop(Argument \n\ must be a positive integer scalar.\n)
if(!is.null(a)  (!is.numeric(a) || length(a) != 1 || a = 0))
stop(Argument \a\ must be a positive numeric scalar.\n)

if(chk==0) {
A - ((1+r/12)^n)*(P - 12*a/r) + 12*a/r
A - max(A,0)
if(isTRUE(all.equal(A,0))) {
nlast - ceiling(Recall(P=P,r=r,a=a))
attributes(nlast) - NULL
} else nlast - NULL
A - c(A=A)
if(!is.null(nlast)) attr(A,lastNonZero) - nlast
return(A)
}

if(is.null(P))
return(c(P=(12*a/r)*(1 - (1+r/12)^(-n

if(is.null(r)) {
if(P/a  n) stop(You would need a negative interest rate!\n)
if(n==1) return(c(r=12*(a-P)/P))
fff - function(r,P,n,a) {
fval - n*log(1+r/12) + log(12*a-r*P) - log(12*a)
J- n/(12+r) - P/(12*a - r*P)
list(fval=fval,jacobian=J)
}
r1 - 12*(1+n/P)/(n-1)
r2 - 0.99*12*a/P
rr - seq(r1,r2,length=100)
ss - fff(rr,P,n,a)$fval
r0 - rr[which.min(abs(ss))]
return(c(r=newt(fff,start=r0,P=P,n=n,a=a)))
}

if(is.null(n)) {
if(r*P = 12*a) return(Inf)
n - (log(12*a) - log(12*a - r*P))/log(1+r/12)
nl - floor(n)
A - Recall(P,r,nl,a)
n - c(n=ceiling(n))
 

Re: [R] How to do a backward calculation for each record in a dataset

2013-02-18 Thread Berend Hasselman

Rolf,

Your attachments got through.
But where is the function newt(…)

Berend

On 18-02-2013, at 21:25, Rolf Turner rolf.tur...@xtra.co.nz wrote:

 
 Some (quite a few!) years ago I wrote myself a wee function called
 compInt() (compound interest) to do --- I think --- just what you require.
 I have attached the code for this function and a help file for it.
 
 If anyone else wants this code, and if the attachments don't get through the 
 list,
 let me know and I can send the stuff to you directly.
 
cheers,
 
Rolf Turner
 
 On 02/18/2013 10:34 PM, Prakasit Singkateera wrote:
 Hi all,
 
 Firstly, it is not a homework. I am working for a hotel booking company in
 Thailand but I don't want to explain a complex equation and concept here so
 I keep it simple and closely related to what I am trying to solve.I apology
 if my question is not clear enough.
 
 I am new to R and previously this problem can be solved easily in Excel
 using the Goal Seek tool. An example related to my question is when we
 use the PMT formula (in Excel) to find the loan payment amount for the
 given values of parameters i.e. interest rate, total number of payments,
 and principal amount of the loan.
 
 loan_payment_amt_of_each_period =
 PMT(interest_rate,total_number_of_payments,principal_amt)
 
 The question is when you know exactly on a monthly basis that you can
 afford only X amount of money to pay and you want to know how many months
 you have to do the payment given your monthly affordable money, the fixed
 interest rate, and the principal amount of loan. Using Goal Seek tool in
 Excel, it is like a backward solving for X given Y by not having to
 transform anything from the original equation. Simply put the
 loan_payment_amt_of_each_period you want and let the software calculate the
 total_number_of_payments for you.
 
 Thanks arun. But that was you solved the original equation and put it as a
 new formula to R to calculate the result which is easy as long as the
 original equation is not complex.
 
 
 Thanks you,
 Prakasit Singkateera
 
 
 On Mon, Feb 18, 2013 at 1:18 AM, Bert Gunter gunter.ber...@gene.com wrote:
 
 Homework? We don't do homework here.
 
 -- Bert
 
 On Sun, Feb 17, 2013 at 5:10 AM, Prakasit Singkateera
 asltjoey.rs...@gmail.com wrote:
 Hi Experts,
 
 I have a dataset of 3 columns:
 
 customer.name product cost
 John Toothpaste 30
 Mike Toothpaste 45
 Peter Toothpaste 40
 
 And I have a function of cost whereby
 
 cost = 3.40 + (1.20 * no.of.orders^2)
 
 I want to do a backward calculation for each records (each customer) to
 find his no.of.orders and create a new column named no.of.orders in
 that
 dataset but I don't know how to do.
 
 Please help me.
 
 Thank you everyone,
 Prakasit
 
 
 --
 
 Bert Gunter
 Genentech Nonclinical Biostatistics
 
 Internal Contact Info:
 Phone: 467-7374
 Website:
 
 http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
 compInt.RcompInt.Rd__
 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.

__
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] How to do a backward calculation for each record in a dataset

2013-02-18 Thread Rolf Turner

On 02/19/2013 09:44 AM, Berend Hasselman wrote:

Rolf,

Your attachments got through.
But where is the function newt(…)?


A --- dang!  It's another item from my personal miscellany package
which I'm so used to having around that I forgot that other people don't 
have it.

Attached.   Along with its documentation file.

Thanks for pointing out my sin of omission. :-)

cheers,

Rolf
newt - function(fn,start,...,eps.p = 1e-08,eps.v = NULL,
 maxit = 50,verb = FALSE)
{
p.o - start
itno - 1
repeat {
fj - fn(p.o,...)
v - fj$fval
t1 - if(is.null(eps.v)) NULL else sum(abs(v))
J - as.matrix(fj$jacobian)
if(qr(J)$rank  ncol(J)) {
cat(Singular Jacobian.\n)
rslt - if(is.null(eps.v)) NA else if(t1  eps.v) p.o
else NA
break
}
else {
p.n - p.o - solve(J) %*% v
t2 - max(abs(p.n - p.o))
if(verb) {
tmp - format(round(c(p.o,p.n,v,t2,t1),6))
np - length(v)
v1 - paste(tmp[1:np],collapse =   )
v2 - paste(tmp[(np + 1):(2 * np)],collapse =   )
v3 - paste(tmp[(2 * np + 1):(3 * np)],collapse =   )
v4 - tmp[3 * np + 1]
v5 - tmp[3 * np + 2]
cat(\nIteration  : ,itno,\n,sep = )
cat(Old par: ,v1,\n,sep = )
cat(New par: ,v2,\n,sep = )
cat(Test ch.par: ,v4,\n,sep = )
cat(Fn. vals.  : ,v3,\n,sep = )
if(!is.null(t1))
  cat(Test f.val: ,v5,\n,sep = )
}
if((!is.null(t1)  t1  eps.v) | t2  eps.p) {
rslt - p.n
break
}
itno - itno + 1
if(itno  maxit) {
cat(Newton's method failed to converge in\n)
cat(maxit,iterations.\n)
rslt - NA
break
}
p.o - p.n
}
}
as.vector(rslt)
}
\name{newt}
\alias{newt}
\title{
Newton's method.
}
\description{
A rather naive general implementation of Newton's method;
but it seems to work.  A lot of the time! [ :-) ]
}
\usage{
newt(fn, start, \dots, eps.p=1e-08, eps.v=NULL,
 maxit=50, verb=FALSE)
}
\arguments{
\item{fn}{
A user-supplied function providing the essential information about
the system of equations to be solved.  The system is assumed to be of
the form \eqn{f(x) = 0} where \eqn{f(x)} is a \eqn{k}-dimensional
function (with components \eqn{f_1(x) \ldots f_k(x)}{f_1(x) ...
f_k(x)} of \eqn{k} variables.

The function \code{fn} must be coded to return a list with components
\code{fval} and \code{jacobian}.  The component \code{fval} must be a
\eqn{k}-dimensional vector (whose \eqn{i^{th}}{i-th} component is the
value of \eqn{f_i(x)}.  The component jacobian is the Jacobian of
the function \eqn{f}, i.e. it is a matrix whose
\eqn{(i,j)^{th}}{(i,j)-th} entry is the derivative of \eqn{f_i(x)}
with respect to \eqn{x_j}.
}
\item{start}{
A \eqn{k}-dimensional vector of starting values from which to
iterate toward the solution.
}
\item{...}{
Any auxilliary arguments needed by the function \code{fn}.
}
\item{eps.p}{
The iteration stops if the maximum absolute value of the
change in the parameters \eqn{x_1, \ldots, x_k}{x_1, ..., x_k}
is less than \code{eps.p}.
}
\item{eps.v}{
If this argument is provided the iteration stops if the sum of the
absolute values of the function values \eqn{f_j(x)} is less than
\code{eps.v}.  (Note: If \code{eps.v} is provided then iteration will
cease if EITHER the \code{eps.p} criterion or the \code{eps.v}
criterion is met.)
}
\item{maxit}{
The maximum number of iterations to attempt before giving
up in disgust.
}
\item{verb}{
Logical scalar; if TRUE a description of the current state
of play is printed out at every iteration.
}}
\value{
If the iterative procedure has converged, to within the specified
tolerance(s), the final value of the k-dimensional vector of
parameter (x) values.  Otherwise, NA.
}
\details{
If the Jacobian becomes (numerically) singular (as determined by the
qv() function) then the function \code{newt} exits.  If \code{eps.v}
is not provided a value of NA is returned.  If \code{eps.v} IS
provided, and if by some miracle the sum of the absolute values of
the function values \eqn{f_j(x)} is less than \code{eps.v}, then the
current value of \eqn{x} is returned (since this \eqn{x} does satisfy
the set of equations to the specified tolerance).
}
\author{Rolf Turner
  \email{r.tur...@auckland.ac.nz}
  \url{http://www.math.unb.ca/~rolf}
}
\examples{
foo - function(x) {
   fval - c(x[1]**2 + x[2]**2 - 1, x[2] - x[1])
   jacobian - matrix(c(2*x[1],2*x[2], -1, 1),byrow=TRUE,ncol=2)
   

Re: [R] How to do a backward calculation for each record in a dataset

2013-02-18 Thread Prakasit Singkateera
Hi Berend,

Thank you for your information. However, I am still not clear on the Rpmt
function please elaborate more about

- What is the purpose of Rpmt function? Why do you subtract
pmt(xrate,x,xpv) from xpmt? / What is the meaning of xpmt ?

- And what is the purpose of doing Rpmt(xpv/xpmt/2,xrate=irate, xpmt=xpmt,
xpv=xpv) and Rpmt(xpv/xpmt*2,xrate=irate, xpmt=xpmt, xpv=xpv) ?

Thank you,
Prakasit



On Tue, Feb 19, 2013 at 9:03 AM, Rolf Turner rolf.tur...@xtra.co.nz wrote:

 On 02/19/2013 09:44 AM, Berend Hasselman wrote:

 Rolf,

 Your attachments got through.
 But where is the function newt(…)?


 A --- dang!  It's another item from my personal miscellany package
 which I'm so used to having around that I forgot that other people don't
 have it.
 Attached.   Along with its documentation file.

 Thanks for pointing out my sin of omission. :-)

 cheers,

 Rolf


[[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] How to do a backward calculation for each record in a dataset

2013-02-17 Thread John Kane
This sounds a bit too much like homework.  

And in any case https://github.com/hadley/devtools/wiki/Reproducibility

John Kane
Kingston ON Canada


 -Original Message-
 From: asltjoey.rs...@gmail.com
 Sent: Sun, 17 Feb 2013 20:10:13 +0700
 To: r-help@r-project.org
 Subject: [R] How to do a backward calculation for each record in a
 dataset
 
 Hi Experts,
 
 I have a dataset of 3 columns:
 
 customer.name product cost
 John Toothpaste 30
 Mike Toothpaste 45
 Peter Toothpaste 40
 
 And I have a function of cost whereby
 
 cost = 3.40 + (1.20 * no.of.orders^2)
 
 I want to do a backward calculation for each records (each customer) to
 find his no.of.orders and create a new column named no.of.orders in
 that
 dataset but I don't know how to do.
 
 Please help me.
 
 Thank you everyone,
 Prakasit
 
   [[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.


FREE 3D MARINE AQUARIUM SCREENSAVER - Watch dolphins, sharks  orcas on your 
desktop!

__
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] How to do a backward calculation for each record in a dataset

2013-02-17 Thread arun
Hi,
I am  not sure I understand it correctly.

dat1-read.table(text=
customer.name    product    cost
John    Toothpaste    30
Mike    Toothpaste    45
Peter    Toothpaste    40
,sep=,header=TRUE,stringsAsFactors=FALSE)
 dat1$no.of.orders-  sqrt((dat1$cost-3.40)/1.20)
 dat1
#  customer.name    product cost no.of.orders
#1  John Toothpaste   30 4.708149
#2  Mike Toothpaste   45 5.887841
#3 Peter Toothpaste   40 5.522681
A.K.





- Original Message -
From: Prakasit Singkateera asltjoey.rs...@gmail.com
To: r-help@r-project.org
Cc: 
Sent: Sunday, February 17, 2013 8:10 AM
Subject: [R] How to do a backward calculation for each record in a dataset

Hi Experts,

I have a dataset of 3 columns:

customer.name     product     cost
John     Toothpaste     30
Mike     Toothpaste     45
Peter     Toothpaste     40

And I have a function of cost whereby

cost = 3.40 + (1.20 * no.of.orders^2)

I want to do a backward calculation for each records (each customer) to
find his no.of.orders and create a new column named no.of.orders in that
dataset but I don't know how to do.

Please help me.

Thank you everyone,
Prakasit

    [[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.


__
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] How to do a backward calculation for each record in a dataset

2013-02-17 Thread Bert Gunter
Homework? We don't do homework here.

-- Bert

On Sun, Feb 17, 2013 at 5:10 AM, Prakasit Singkateera
asltjoey.rs...@gmail.com wrote:
 Hi Experts,

 I have a dataset of 3 columns:

 customer.name product cost
 John Toothpaste 30
 Mike Toothpaste 45
 Peter Toothpaste 40

 And I have a function of cost whereby

 cost = 3.40 + (1.20 * no.of.orders^2)

 I want to do a backward calculation for each records (each customer) to
 find his no.of.orders and create a new column named no.of.orders in that
 dataset but I don't know how to do.

 Please help me.

 Thank you everyone,
 Prakasit

 [[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.



-- 

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm

__
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.