Probit avec R : Différence entre versions

De Wiki ODR
Aller à : navigation, rechercher
(Fichier PHP d'appel du script R pour produire le traitement)
Ligne 63 : Ligne 63 :
 
$t = time() ;
 
$t = time() ;
  
echo "lancement du traitement : probit sous R<br><br>" ;
+
echo "lancement du traitement : probit sous R <p />" ;
flush();
 
  
echo " Ouverture en lecture du fichier de commande R<BR><BR>";
+
echo " Ouverture en lecture du fichier de commande R <p />";
flush();
 
  
 
if ( !($fichier_R = fopen ('probit2.par', 'r')))
 
if ( !($fichier_R = fopen ('probit2.par', 'r')))
Ligne 80 : Ligne 78 :
 
$mysqldata = "c:\\easyphp\\mysql\\data\\$base\\";
 
$mysqldata = "c:\\easyphp\\mysql\\data\\$base\\";
  
echo "traitement sous r (long) <BR><BR>" ;
+
echo "traitement sous r (long) <p />" ;
flush();
 
  
 
// exécution de R
 
// exécution de R
Ligne 88 : Ligne 85 :
  
 
// affichage
 
// affichage
echo "Impression des resultats dans un tableau <BR><BR>" ;
+
echo "Impression des resultats dans un tableau <p />" ;
 
flush();
 
flush();
  
 +
// lecture des lignes du fichier résultat probitessai.txt généré par la fonction R
 
if ( !($fic_sortie = fopen ('C:\\Documents and Settings\\cedric\\Bureau\\probitessai.txt', 'r')))
 
if ( !($fic_sortie = fopen ('C:\\Documents and Settings\\cedric\\Bureau\\probitessai.txt', 'r')))
 
{
 
{
Ligne 110 : Ligne 108 :
 
}
 
}
  
// entête du tableaux
+
// entête du tableau HTML
 
echo "<TABLE border=0 cellspacing='0' cellpading='5' >";
 
echo "<TABLE border=0 cellspacing='0' cellpading='5' >";
 
echo "<tr BGCOLOR = #009900><TD colspan='11'></TD></TR>";
 
echo "<tr BGCOLOR = #009900><TD colspan='11'></TD></TR>";
Ligne 130 : Ligne 128 :
 
}
 
}
  
// cloture du tableau
+
// fermeture du tableau
 
echo "<tr BGCOLOR = #009900><TD colspan='11'></TD></TR>";
 
echo "<tr BGCOLOR = #009900><TD colspan='11'></TD></TR>";
 
echo"</TABLE>";
 
echo"</TABLE>";
Ligne 141 : Ligne 139 :
 
?>
 
?>
 
</pre>
 
</pre>
 
  
 
==== Résultats ====
 
==== Résultats ====

Version du 26 avril 2007 à 10:36

R propose des fonctions très générales, les modèles utilisés en économétries sont considérés comme des cas particuliers

Un probit est considéré comme cas particulier des modèles linéaires généralisés. Il s'agit d'un modèle binomial (family=binomial) et la fonction de transformation utilisée est la normale (link=probit)

Les deux scripts suivants sont des tests pour l'appel d'une fonction probit avec R sous php et l'exploitation de données mysql. L'objectif ici est d'expliquer la contractualisation des mae par des exploitants selon différents critères :

  • la SAU de l'exploitation
  • age du chef d'exploitation
  • le type de société (individuel ou non)
  • contractualise une ichn
  • est en zone défavorisée ou montagne (inat)

fichier d'appel de la fonction probit dans R

#probit2.par

library(RODBC)

channel1 <- odbcConnect("myodbc", "")

objA <- sqlQuery(channel1, "select * from probit.probit ")

# probit 
res <- glm(mae ~ sau + age + fsoc1 + ichn + inat1 + inat2, family=binomial(link=probit), data = objA)

res2 <- summary(res)

# récupération du tableau des estimations
trait <- res2$coefficients

# test khi2 à vérifier (normalement pour poisson)
#X2 <- sum(residuals(res, type ="pearson")^2)
#ddl <- df.residual(res)
#chi <- 1 - pchisq(X2, ddl)

# création du fichier texte
db <- "c:/Documents and Settings/cedric/Bureau/probitessai.txt"

# écriture du fichier texte
write.table(x=trait,file=db,quote=FALSE,sep=";",row.names=TRUE,col.names=FALSE)

odbcClose(channel1)

q()



# autre requête à tester
house.probit<-polr(mae ~ sau + age + fsoc1 + ichn + inat1 + inat2, data = objA, weights = Freq, method = "probit") 


Fichier PHP d'appel du script R pour produire le traitement

utilisation d'easyphp en local

<?

// probit sous R
// Connexion BD test
//http://localhost/test_probit_cedric.php

$t = time() ;

echo "lancement du traitement : probit sous R <p />" ;

echo " Ouverture en lecture du fichier de commande R <p />";

if ( !($fichier_R = fopen ('probit2.par', 'r')))
	{
	print("impossible d'ouvrir le fichier probit2.par");
	exit;
	}


fclose ($fichier_R);

$mysqldata = "c:\\easyphp\\mysql\\data\\$base\\";

echo "traitement sous r (long) <p />" ;

// exécution de R
$ATraiter = "c:\\rw2001\\bin\\Rterm.exe -q --no-restore --no-save < " . "probit2.par" . ">". "fichier_sortie";
exec ($ATraiter);

// affichage
echo "Impression des resultats dans un tableau <p />" ;
flush();

// lecture des lignes du fichier résultat probitessai.txt généré par la fonction R
if ( !($fic_sortie = fopen ('C:\\Documents and Settings\\cedric\\Bureau\\probitessai.txt', 'r')))
	{
	print("impossible d'ouvrir le fichier fichier_sortie");
	exit;
	}

while (!feof($fic_sortie))
	{
	$ligne = fgets ($fic_sortie, 500);
	$ligne = trim($ligne) ;
	if($ligne == '' ) continue ;
	$ligne = explode(";" ,$ligne);
	$var[] = $ligne[0] ;
	$estim[] = $ligne[1] ;
	$stderror[] = $ligne[2];
	$zvalue[] = $ligne[3];
	$prz[] = $ligne[4];
	}

// entête du tableau HTML
echo "<TABLE border=0 cellspacing='0' cellpading='5' >";
echo "<tr BGCOLOR = #009900><TD colspan='11'></TD></TR>";
echo "<TR bgcolor = #EAFCEA align='center'><TD BGCOLOR = #009900 width = '0px' ></TD><TD></TD><TD BGCOLOR = #009900 ></TD><TD >Estimate</TD> <TD BGCOLOR = #009900 width = '0px' ></TD> <TD>Std. Error</TD><TD BGCOLOR = #009900 width = '0px' ></TD><TD>z value</TD><TD BGCOLOR = #009900 ></TD><TD>Pr(>|z|)</TD><TD BGCOLOR = #009900 width = '0px' ></TD></TR>";
echo "<tr BGCOLOR = #009900 width = '0px' ><TD colspan='11'></TD></TR>";

// lignes du tableau
$i = 0 ;
while ( $i < count($var) )
	{
	
	 echo "<TR><TD BGCOLOR = #009900 ></TD><TD>".$var[$i]."</TD>"; 
	 echo "<TD BGCOLOR = #009900 ></TD><TD Align = 'right' width = '20%' >".sprintf( "%.7f",$estim[$i])."</TD>"; 
	 echo "<TD BGCOLOR = #009900 ></TD><TD Align = 'right' width = '20%' >".sprintf( "%.7f",$stderror[$i])."</TD>";
	 echo "<TD BGCOLOR = #009900 ></TD><TD Align = 'right' width = '20%' >".sprintf( "%.2f",$zvalue[$i])."</TD>";
	 echo "<TD BGCOLOR = #009900 ></TD><TD Align = 'right' width = '20%' >".sprintf( "%.3f",$prz[$i])."</TD>";
	 echo "<TD BGCOLOR = #009900></TD></TR>" ;
	$i++;
	}

// fermeture du tableau
echo "<tr BGCOLOR = #009900><TD colspan='11'></TD></TR>";
echo"</TABLE>";

$t1 = time() ;
$temps = $t1 - $t ;
echo "<BR> $temps secondes" ;


?>

Résultats

Les résultats apparaissent dans le navigateur web sous cette forme :

Exemple probit.jpg