Learn R Programming

GA4Stratification (version 1.0)

GA4StratificationP2fit: The genetic algorithm (GA) fitness function that calculates the variance of the estimate of each chromosome in the GA generation with an Proportional Sample Allocation Scheme

Description

This is the fitness function in GA that calculates the variance of the estimate according to the boundaries obtained with GA and sample sizes obtained with Proportional Allocation.

Usage

GA4StratificationP2fit(randomGeneration, dataName, numberOfStrata, sampleSize, cumTotal, sumSquares, c, dd, nocrom, fitp1, fit, N, means, s, n, vars, mas, NN, k, p, t)

Arguments

randomGeneration
The generation which a fitness function will be applied and a fitness value will be calculated. This is initially a random generation and after each iteration it is the mutated, crossovered and selected generation.
dataName
A string: The name of any type of numeric data containing the values of a univariate stratification variable.
numberOfStrata
An integer: The number of strata.
sampleSize
An integer: The total sample size.
cumTotal
An array: The cumulative total of the data elements from i=1 to i=N
sumSquares
An array: The cumulative total of the squares of the data elements from i=1 to i=N
c
An integer: The length of the data.
dd
An array (nocrom X 1): The minimum of the difference in between the size of each stratum and the sample size to be drawn from that stratum.
nocrom
An integer: The number of the chromosomes in the generation.
fitp1
An array (1 X nocrom): The fitness value for each chromosome in the generation.
fit
An array (nocrom X 1): The fitness value for each chromosome in the generation.
N
An array (nocrom X nofstrata): The number of the elements in each stratum for each chromosome in the generation.
means
An array (nocrom X nofstrata): The mean of the elements in each stratum for each chromosome in the generation.
s
An array (nocrom X nofstrata): The standart deviation of the elements in each stratum for each chromosome in the generation.
n
An array (nocrom X nofstrata): The number of the sample size to be drawn from each stratum for each chromosome in the generation.
vars
An array (nocrom X nofstrata): The variance of the estimate in each stratum for each chromosome in the generation.
mas
An array (nocrom X nofstrata): The indice of each stratum for each chromosome in the generation.
NN
An array (nocrom X nofstrata): The cumulative sum of the number of the elements in each stratum.
k
An array (nocrom X nofstrata): The difference between the number of the elements and the sample sizes in each stratum.
t
An array (nocrom X nofstrata): The maximum of the k
p
An array (nocrom X nofstrata): The indice of the element where k is equal to t

References

http://ideas.repec.org/a/eee/csdana/v52y2007i1p53-67.html

http://www.sciencedirect.com/science/article/B6V8V-4NHM520-1/2/a21e0295aa1616ff56da1ddf2c0ba1ac

See Also

GA4StratificationP2 GA4StratificationP2fitt GA4StratificationP2m GA4StratificationP2x

GA4StratificationSelection randomnumGenerator

Examples

Run this code
## The function is currently defined as
function(randomGeneration,dataName,numberOfStrata,sampleSize,cumTotal,sumSquares,c,dd,nocrom,fitp1,fit,N,means,s,n,vars,mas,NN,k,p,t)
{
   for ( i in 1:nocrom )
   {
   	mas[i,]=which(randomGeneration[i,]==1,arr.ind=TRUE)
    	N[i,1]=min(mas[i,])
    	means[i,1]=cumTotal[mas[i,1],]/N[i,1]
      if(N[i,1]==1)
	{s[i,1]=0
	} else
	{
    	s[i,1]=((N[i,1]/(N[i,1]-1))*(sumSquares[N[i,1]]/N[i,1]-means[i,1]^2))^.5
	}
    	for ( j in 2:numberOfStrata )
	{    
    		N[i,j]=mas[i,j]-mas[i,(j-1)]
      	means[i,j]=(cumTotal[mas[i,j],]-cumTotal[mas[i,j-1],])/N[i,j]
      	if(N[i,j]==1)
		{s[i,j]=0
		} else
		{

      		s[i,j]=((N[i,j]/(N[i,j]-1))*((sumSquares[mas[i,j]]-sumSquares[mas[i,j-1]])/N[i,j]-means[i,j]^2))^.5
		}
	}   

    	for ( j in 1:numberOfStrata )
		{
      		n[i,j]=max(1,floor(sampleSize*N[i,j]/sum(N[i,])))
        	n[i,j]=min(n[i,j],N[i,j])
		}
		if ( sampleSize-sum(n[i,])>0 )
		{
      	k[i,]=N[i,]-n[i,]
        	t[i,]=max(k[i,])
        	p[i,]=which(k[i,]==t[i,],arr.ind=TRUE)[1]
        	n[i,p[i,1]]=min(n[i,p[i,1]]+sampleSize-sum(n[i,]),N[i,p[i,1]])
		}

		for ( j in 1:numberOfStrata )
		{
        	vars[i,j]=((N[i,j]-n[i,j])*s[i,j]^2*N[i,j]^2)/(c^2*n[i,j]*N[i,j])
		}

    	dd[i,]=min((N[i,]-n[i,]))
    	NN[i,]=cumsum(N[i,])
    	kl=0

   		fit[i,]=sum(vars[i,])

    	if ( dd[i]<0 ) 
		{
			fit[i]= 9999999999999999
    	} else if (!all(N[i,]!=1)) 
		{
			fit[i]= 999999999999999999
    	} else if (!all(N[i,]!=0)) 
		{
			fit[i]= 999999999999999999
		} else
		{
			fit[i]=fit[i]
		}
   
    	for ( j in 1:(numberOfStrata-1) )
		{
        	kl=kl+dataName[(NN[i,j]+1),]-dataName[NN[i,j],]
		}

    	p2fit=array(-fit,dim=c(nocrom,1))

   }
   return(p2fit)
}

Run the code above in your browser using DataLab