Probit avec R : Différence entre versions

De Wiki ODR
Aller à : navigation, rechercher
 
(9 révisions intermédiaires par le même utilisateur non affichées)
Ligne 3 : Ligne 3 :
 
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)
 
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 :
+
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 de mesures herbagères par des éleveurs (200 000 bénéficiaires sélectionnés) par différents critères :
 
* la SAU de l'exploitation
 
* la SAU de l'exploitation
 
* age du chef d'exploitation
 
* age du chef d'exploitation
 
* le type de société (individuel ou non)
 
* le type de société (individuel ou non)
 
* contractualise une ichn
 
* contractualise une ichn
* est en zone défavorisée ou montagne (inat)
+
* est en zone défavorisée ou montagne (inat1 et inat2)
  
===fichier d'appel de la fonction probit dans R===
+
====fichier d'appel de la fonction probit dans R====
  
 
<pre>
 
<pre>
 
#probit2.par
 
#probit2.par
 +
#tests pour probit, Cédric janvier 2007
  
 
library(RODBC)
 
library(RODBC)
Ligne 51 : Ligne 52 :
  
  
===Fichier PHP d'appel du script R pour produire le traitement ===
+
====Fichier PHP d'appel du script R pour produire le traitement ====
  
 
utilisation d'easyphp en local
 
utilisation d'easyphp en local
Ligne 57 : Ligne 58 :
 
<pre><?
 
<pre><?
  
// probit sous R
+
/*
// Connexion BD test
+
probit sous R
//http://localhost/test_probit_cedric.php
+
Connexion BD test
 +
http://localhost/test_probit_cedric.php
 +
Cédric Janvier 2007
 +
*/
  
 
$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 82 :
 
$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 89 :
  
 
// 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
 
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')))
 
{
 
{
print("impossible d'ouvrir le fichier fichier_sortie");
+
print("impossible d'ouvrir le fichier probitessai");
 
exit;
 
exit;
 
}
 
}
Ligne 110 : Ligne 112 :
 
}
 
}
  
// 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 = #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><td colspan='11'></td></tr>";
echo "<tr BGCOLOR = #009900 width = '0px' ><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
 
// lignes du tableau
Ligne 121 : Ligne 133 :
 
{
 
{
 
 
echo "<TR><TD BGCOLOR = #009900 ></TD><TD>".$var[$i]."</TD>";  
+
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%' >"
echo "<TD BGCOLOR = #009900 ></TD><TD Align = 'right' width = '20%' >".sprintf( "%.7f",$stderror[$i])."</TD>";
+
        .sprintf( "%.7f",$estim[$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%' >"
echo "<TD BGCOLOR = #009900 ></TD><TD Align = 'right' width = '20%' >".sprintf( "%.3f",$prz[$i])."</TD>";
+
        .sprintf( "%.7f",$stderror[$i])."</td>";
echo "<TD BGCOLOR = #009900></TD></TR>" ;
+
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++;
 
$i++;
 
}
 
}
  
// 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 157 :
 
?>
 
?>
 
</pre>
 
</pre>
 +
 +
==== Résultats ====
  
 
Les résultats apparaissent dans le navigateur web sous cette forme :
 
Les résultats apparaissent dans le navigateur web sous cette forme :
  
 
[[Image:Exemple_probit.jpg]]
 
[[Image:Exemple_probit.jpg]]

Version actuelle datée du 26 avril 2007 à 11:46

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 de mesures herbagères par des éleveurs (200 000 bénéficiaires sélectionnés) par 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 (inat1 et inat2)

fichier d'appel de la fonction probit dans R

#probit2.par
#tests pour probit, Cédric janvier 2007

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
Cédric Janvier 2007
*/

$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
if ( !($fic_sortie = fopen ('C:\\Documents and Settings\\cedric\\Bureau\\probitessai.txt', 'r')))
	{
	print("impossible d'ouvrir le fichier probitessai");
	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