[R] Functions ,Optim, & Dataframe
Michael Papenfus
mmpapenf at wisc.edu
Mon Jul 31 07:46:50 CEST 2006
I have defined the following function:
fr<-function(x) {
u<-x[1]
v<-x[2]
sqrt(sum((plnorm(c(3,6),u,v)-c(.55,.85))^2))
}
which I then solve using optim
y<-optim(c(1,1),fr)
> y$par
[1] 1.0029771 0.7610545
This works fine.
Now I want to use these two steps on a dataframe:
mydat<-data.frame(d1=c(3,5),d2=c(6,10),p1=c(.55,.05),p2=c(.85,.35))
> mydat
d1 d2 p1 p2
1 3 6 0.55 0.85
2 5 10 0.05 0.35
where for each row in mydat, I append the two parameter resulting from
optim into mydat.
I want to do this for a larger dataset but thought I would start with a
simple two row dataframe.
I have tried this with loops and the apply function, but seem to be
getting nowhere.
Thanks for any input.
mike
mmpapenf at wisc.edu
More information about the R-help
mailing list