similar to: Trouble with optim function

Displaying 20 results from an estimated 2000 matches similar to: "Trouble with optim function"

2011 Apr 26
7
Second largest element from each matrix row
Hi, I need to extract the second largest element from each row of a matrix. Below is my solution, but I think there should be a more efficient way to accomplish the same, or not? set.seed(1) a <- matrix(rnorm(9), 3 ,3) sec.large <- as.vector(apply(a, 1, order, decreasing=T)[2,]) ans <- sapply(1:length(sec.large), function(i) a[i, sec.large[i]]) ans Thanks in advance for your
2004 Jul 03
4
counting the occurrences of vectors
Hi: I have two matrices, A and B, where A is n x k, and B is m x k, where n >> m >> k. Is there a computationally fast way to count the number of times each row (a k-vector) of B occurs in A? Thanks for any suggestions. Best, Ravi. [[alternative HTML version deleted]]
2012 Aug 04
3
Head or Tails game
Hi, Reading about a "Heads and Tails" game in http://www.dartmouth.edu/~chance/teaching_aids/books_articles/probability_book/amsbook.mac.pdf Introduction to Probability (Example 1.4, pp. 5-8). You toss a coin 40 times. If heads, Peter wins $1, tails, he loses $1. I think I can do that ok with: winnings <- sum(sample(c(-1,1), 40, replace=TRUE)) But I have to do it 10,000 times
2010 Jun 29
1
Performance enhancement for ave
library(plyr) n<-100000 grp1<-sample(1:750, n, replace=T) grp2<-sample(1:750, n, replace=T) d<-data.frame(x=rnorm(n), y=rnorm(n), grp1=grp1, grp2=grp2) system.time({ d$avx1 <- ave(d$x, list(d$grp1, d$grp2)) d$avy1 <- ave(d$y, list(d$grp1, d$grp2)) }) # user system elapsed # 39.300 0.279 40.809 system.time({ d$avx2 <- ave(d$x, interaction(d$grp1, d$grp2, drop =
2010 Feb 12
1
paired wilcox test on each row of a large dataframe
hI I have to calculate V statistic for each row of a large dataframe (28000). I can not use multtest package for paired wilcox test. I have been using for loop which are. Is there a way to speed the computation with another method like using apply or tapply? My data set looks like this: 11573_MB 11911_MB 11966_MB 12091_MB 12168_MB 12420_MB................ cg00000292
2012 Aug 04
1
how to coerce the result of sweep to be an array if result of FUN is a string?
Hi, I would like to use sweep to "sweep out" proportions and confidence intervals for an array, however when I supply a function which returns a string (containing something like "9% (3-18%)") I get back a list instead of an array, here is a simplified example: # example showing that sweep does not return an array with same dimensions as STATS as advertised
2004 Jul 01
3
list structure question
Hi, I have a list in which element is a vector (all of the same length and all numeric). I want to find the mean of the first elements of the vectors, the mean of the second elements of the vectors and so on. Currently I convert the list to a data.frame and apply rowMeans(). But is there a way to to do this directly on the list? I seem to recall a post in which there was such a function (or
2008 Aug 27
5
Integrate a 1-variable function with 1 parameter (Jose L. Romero)
Hey fellas: I would like to integrate the following function: integrand <- function (x,t) { exp(-2*t)*(2*t)^x/(10*factorial(x)) } with respect to the t variable, from 0 to 10. The variable x here works as a parameter: I would like to integrate the said function for each value of x in 0,1,..,44. I have tried Vectorize to no avail. Thanks in advance, jose romero
2010 Dec 15
3
Applying function to a TABLE and also "apply, tapply, sapply etc"
Dear R-help forum members, Suppose I have a data-frame having two variables and single data for each of them, as described below. variable_1           variable_2         10                          20 I have written a function, say, 'fun' which uses input 10 and 20 and gives me desired result. fun = function(X, Y)          {          X + Y              #( I am just giving an example of
2004 Dec 16
8
counting numbers without replicates in a vector
Hi, I am just wondering if there is an easy way to count in a numeric vector how many numbers don't have replicates. For example, a=c(1,1,2,2,3,4,5), how can I know there are three numbers (3, 4 and 5) without replicates? Thank you! Jun =====
2007 Feb 08
2
Timings of function execution in R [was Re: R in Industry]
On 2/8/07, Albrecht, Dr. Stefan (AZ Private Equity Partner) <stefan.albrecht at apep.com> wrote: > Dear all, > > Thanks a lot for your comments. > > I very well agree with you that writing efficient code is about optimisation. The most important rules I know would be: > - vectorization > - pre-definition of vectors, etc. > - use matrix instead of data.frame > - do
2009 Dec 08
4
lower.tail option in pnorm
Hi, I would have thought that these two constructions would produce the same result but they do not. Resp <- rbinom(10, 1, 0.5) Stim <- rep(0:1, 5) mm <- model.matrix(~ Stim) Xb <- mm %*% c(0, 1) ifelse(Resp, log(pnorm(Xb)), log(1 - pnorm(Xb))) pnorm(as.vector(Xb), lower.tail = Resp, log.p = TRUE) > ifelse(Resp, log(pnorm(Xb)), log(1 - pnorm(Xb))) [1] -0.6931472 -1.8410216
2011 Sep 02
5
Hessian Matrix Issue
Dear All, I am running a simulation to obtain coverage probability of Wald type confidence intervals for my parameter d in a function of two parameters (mu,d). I am optimizing it using "optim" method "L-BFGS-B" to obtain MLE. As, I want to invert the Hessian matrix to get Standard errors of the two parameter estimates. However, my Hessian matrix at times becomes
2009 Nov 02
2
a prolem with constrOptim
Hi, I apologize for the long message but the problem I encountered can't be stated in a few lines. I am having some problems with the function constrOptim. My goal is to maximize the likelihood of product of K multinomials, each with four catagories under linear constraints on the parameter values. I have found that the function does not work for many data configurations. #The likelihood
2008 Jun 03
3
How to solve a non-linear system of equations using R
Dear R-list members, I've had a hard time trying to solve a non-linear system (nls) of equations which structure for the equation i, i=1,...,4, is as follows: f_i(d_1,d_2,d_3,d_4)-k_i(l,m,s) = 0 (1) In the expression above, both f_i and k_i are known functions and l, m and s are known constants. I would like to estimate the vector d=(d_1,d_2,d_3,d_4) which is solution
2010 Sep 21
2
Trouble with Optimization in "Alabama" Package
Hello, This is my first post to the help request list, so I'm going to err on the side of giving too much information. I'm working on writing a simulation in which agents will make repeated production and exchange decisions with randomly chosen partners. The idea is, all agents can produce two goods which they want to consume, they choose a value t in [0,10] which sets their production
2005 Aug 16
2
specify seed for Random Number Generator
I need to generate 100 I.I.D samples from an exponential distribution. I use rexp(100,parameter). Is there anyway to specify a seed to determine the first input for the uniform random number generator used to generate these exponentials? -Dhiren
2011 Feb 02
2
clustering with finite mixture model
Dear R-help, I am doing clustering via finite mixture model. Please suggest some packages in R to find clusters via finite mixture model with continuous variables. And also I wish to verify the distributional properties of the mixture distributions by fitting the model with lognormal, gamma, exponentials etc,. Thanks in advance,  warm regards,Ms.Karunambigai M PhD Scholar Dept. of Biostatistics
2010 Jul 06
2
numerical derivative R help
I fit my CDF to sum of exponentials and now I want to take the numerical derivative of this function to obtain probability density.I will really appreciate your help reagrding the error messages I am getting which I don't understand. * * > fitterma <- function(xtime) { a <- -0.09144115 b <- -0.01335756 c <- -2.368057 d <- -0.00600052
2012 Dec 16
1
nls for sum of exponentials
Hi there, I am trying to fit the following model with a sum of exponentials - y ~ Ae^(-md) + B e^(-nd) + c the model has 5 parameters A, b, m, n, c I am using nls to fit the data and I am using DEoptim package to pick the most optimal start values - fm4 <- function(x) x[1] + x[2]*exp(x[3] * -dist) + x[4]*exp(x[5] * -dist) fm5 <- function(x) sum((wcorr-fm4(x))^2) fm6 <- DEoptim(fm5,