Probit avec R : Différence entre versions
De Wiki ODR
Ligne 1 : | Ligne 1 : | ||
+ | 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. | ||
+ | |||
+ | fichier d'appel de la fonction probit dans R | ||
+ | |||
+ | <pre> | ||
+ | #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") | ||
+ | </pre> | ||
+ | |||
+ | |||
+ | Fichier PHP d'appel du script R pour produire le traitement :<br /> | ||
+ | utilisation d'easyphp en local | ||
+ | |||
<pre><? | <pre><? | ||
Version du 26 avril 2007 à 10:23
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.
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<br><br>" ; flush(); echo " Ouverture en lecture du fichier de commande R<BR><BR>"; flush(); 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) <BR><BR>" ; flush(); // 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 <BR><BR>" ; flush(); 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 tableaux 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++; } // cloture du tableau echo "<tr BGCOLOR = #009900><TD colspan='11'></TD></TR>"; echo"</TABLE>"; $t1 = time() ; $temps = $t1 - $t ; echo "<BR> $temps secondes" ; ?>