GenEstim : A simple genetic algorithm for parameters estimation

October 18, 2009

(This article was first published on [R] tricks, and kindly contributed to R-bloggers)

The GenEstim function presented here uses a very simple genetic algorithm to estimate parameters. The function returns the best estimated set of parameters ($estim), the AIC ($information) at each generation, and the cost of the best model ($bestcost) at each generation.

Results of running the program with a logistic function :

Logis = function(x,p)	p[[1]]/(1+p[[2]]*exp(-p[[3]]*x))
RSS = function(par)	sum((Logis(X,par)-Y)^2)
aic <- function(yvalues,rss,par)
	k <- length(par)
	n <- length(yvalues)
	aic <- 2*k+n*log(rss/n)
P	<-	list(2,10,4)
X	<-	seq(from=-5,to=5,by=0.1)
Y	<-	Logis(X,P) + rnorm(length(X),sd=0.1)
GenEstim	<- function(,
		cost = RSS,
		numiter = 1e3,
		npop = 1e2)
	bestcost <- NULL
	cur.AIC <- NULL
	for(it in 1:numiter)
		pop <- matrix(0,ncol=length(,nrow=npop)
		for(p in 1:length(
			pop[,p] 	<- rnorm(npop,[[p]],sd=1)
			pop[1,p]	<-[[p]]
		Costs <- NULL
		for(i in 1:nrow(pop))
			li <- as.list(pop[i,])
			Costs[i] <- cost(li)
		bestcost <- c(bestcost,min(Costs))
		best <-which.min(Costs) <- as.list(pop[best,])
		cur.AIC <- c(cur.AIC,aic(Y,RSS(,

simul <- GenEstim(list(0,0,0))
x.control <- seq(from=-6,to=6,by=0.1)

To leave a comment for the author, please follow the link and comment on their blog: [R] tricks. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)