Ejemplo n.º 1
0
void FindHierarchicalModels(CRegression* allregs,CData& Data,char* DataFileName,int target,int nMaxRegressors)
{
	int i,j;
	char buffer[2048];
	FILE* out = NULL;
	int responseVariable = target-1;
	int lenmodel = 1+nMaxRegressors + Data.NumOfConfoundingVars; // + all confounding vars
	int* amodel = new int[lenmodel];
	CRegression* p = allregs->Next;
	
	int modelid = 1;
	while(NULL!=p)
	{
		set<int>::iterator it;
		lenmodel = 0;
		for(it=(p->Vars).begin();it!=(p->Vars).end();it++)
		{
			amodel[lenmodel] = *it;
			lenmodel++;
		}

		// Next insert all the confounding vars, followed by the response var
	        j = Data.NumOfConfoundingVars;
        	for(i=0; i<=j; i++)
        	{
                	amodel[lenmodel] = responseVariable-j+i;
			lenmodel++;
        	}

		//amodel[lenmodel] = responseVariable;
		//lenmodel++;
		
		printf("Model [%d] ::",modelid);
		for(i=0;i<lenmodel;i++) printf(" %d",amodel[i]);
		printf("\n");
		
		//get the marginal table for these variables
		LPTable dataTable = new Table;
		int* index = new int[lenmodel];
		for(j=0;j<lenmodel;j++)
		{
			index[j] = 2;
		}
		if(!dataTable->Alloc(index,lenmodel))
		{
			printf("Error allocating memory!\n");
			exit(1);
		}
		for(i=0;i<Data.SampleSize;i++)
		{
			for(j=0;j<lenmodel;j++)
			{
				index[j] = (int) (Data.data[i][amodel[j]]);
			}
			dataTable->SetIndex(index);
			dataTable->Set(dataTable->Get()+1);
		}
		
		//file where the best model will be saved
		sprintf(buffer,"%s.shotgun.%d.%d.reg.model%d.txt",DataFileName,target,nMaxRegressors,modelid);
		if(NULL==(out=fopen(buffer,"w"))) 
		{
			printf("Cannot open file [%s]\n",buffer);
			return;
		}
		
		//do the shotgun search
		TableShotgunSearch(out, dataTable, model.mnShotgunChainReplicates,
			model.mdShotgunCutoffMax, model.mdShotgunCutoffMin, model.mdShotgunProbMax, Data.NumOfConfoundingVars);
		fclose(out);
		
		//clean memory
		dataTable->Reset();
		delete dataTable; dataTable = NULL;
		
		//go to the next model
		p = p->Next;
		modelid++;
	}
	//clean memory
	delete[] amodel; amodel = NULL;
	return;
}
Ejemplo n.º 2
0
void getNextTheta(int** VarSets,int* lenVarSets,int nVarSets,
                  int* amodel,int lenCL,int* CL,LPNTable smalltheta,LPTable Theta,LPTable nextTheta)
{
	int i,iF;
	int okay1;
	int iE, iL, iC;
	const int NTD = nextTheta->nDimens;
	int* CLcomplement = new int[NTD];
	int* CLfull = new int[NTD];
	int* FunionL = new int[NTD];
	const int TT = Theta->Total;
	double g[TT];
	int len[TT];
	
	for(i=0;i<NTD;i++)
	{
		CLcomplement[i] = 1;
		CLfull[i] = 0;
	}
	for(i=0;i<lenCL;i++)
	{
		CLcomplement[CL[i]] = 0;
		CLfull[CL[i]] = 1;
	}
	
	for(i=0;i<nextTheta->Total;i++)
	{
		nextTheta->Data[i] = Theta->Data[i];
		g[i] = 0;
		len[i]=0;
	}
	
	////////////////////////////////////////////////
	Theta->GetFirst();
	okay1 = 1;
	iF = 0;
	while(okay1)
	{
		len[iF] = lenVarSets[iF];
										
		double s1 = 1.0;
		for(iL=1;iL<nVarSets;iL++)
		{	
			if(subset(Theta->nDimens,VarSets[iL],CLcomplement))
			{
				for(i=0;i<Theta->nDimens;i++)
				{
					FunionL[i] = 0;
					if(VarSets[iL][i]==1) FunionL[i] = 1;
					if(Theta->Index[i]==1) FunionL[i] = 1;
				}
				
				int thereisone = 0;
				double sC = 0.0;
				for(iC=1;iC<nVarSets;iC++)
				{
					if(0==subset(Theta->nDimens,VarSets[iC],Theta->Index))
					{
						if(1==subset(Theta->nDimens,VarSets[iC],FunionL))
						{
							sC += Theta->GetI(VarSets[iC]);
							thereisone = 1;
						}
					}
				}
				if(thereisone)
				{
					s1 += exp(sC);
				}
			}
		}
		g[iF]=log(s1);
		iF++;
		okay1 = Theta->GetNext();
	}
	////////////////////////////////////////////////
	
	nextTheta->GetFirst();
	nextTheta->Set(0);
	iE = 0;
	while(nextTheta->GetNext())
	{
	    iE++;
		if(amodel[iE])
		{
		    if(1==subset(Theta->nDimens,nextTheta->Index,CLfull))
			{
				double s0 = smalltheta->GetI(nextTheta->Index);
				int lenE = 0;
				for(i=0;i<NTD;i++) lenE+=nextTheta->Index[i];
		
				Theta->GetFirst();
				int okay1 = 1;
				iF = 0;
				while(okay1)
				{
					if(len[iF]>=0)
					{
						if(subset(Theta->nDimens,Theta->Index,nextTheta->Index))
						{
							s0 += pow(-1,lenE-len[iF]-1)*g[iF];
						}
					}
					iF++;
					okay1 = Theta->GetNext();
				}
				nextTheta->Set(s0);
			}
			else
			{
				Theta->SetIndex(nextTheta->Index);
				nextTheta->Set(Theta->Get());
			}
		}
		else
		{
			nextTheta->Set(0);
		}
	}

	delete[] CLcomplement; CLcomplement = NULL;
	delete[] CLfull; CLfull = NULL;
	delete[] FunionL; FunionL = NULL;
	return;
}