Contact Us!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

| Download

All published worksheets from http://sagenb.org

Views: 184312
Image: ubuntu2004
︠af610eb2-a0cb-4650-adc8-d47fbd2f759ei︠
%html
<h1 class="title">Sage peut interagir avec R pour faire des statistiques</h1>
<p><span>Chaque copie de sage poss&egrave;de une copie de R, un logiciel de traitements statistiques open-source puissant et reconnu par la communaut&eacute; math&eacute;matique. Il est possible d'utiliser R directement dans une cellule du Notebook:&nbsp;</span></p>

︡dee6c9a9-ec62-429d-ba0d-cb6f531fbebf︡{"html": "<h1 class=\"title\">Sage peut interagir avec R pour faire des statistiques</h1>\n<p><span>Chaque copie de sage poss&egrave;de une copie de R, un logiciel de traitements statistiques open-source puissant et reconnu par la communaut&eacute; math&eacute;matique. Il est possible d'utiliser R directement dans une cellule du Notebook:&nbsp;</span></p>"}︡
︠179dedfc-0a5b-4c06-b4eb-1de3d5fab9ac︠
%r
x <- c(1,3,5,6,4,9,9,1,1,6,7,8,4,5,5)
︡bd51b8c1-cedb-4389-9c96-d5240068d685︡︡
︠4f784afd-8cf4-49b8-ac80-fc2fa2bb0540i︠
%html
<p>Bon, je ne suis pas un statisticien et ne fait pas de la statistique de haut vol ici. On calcule la moyenne, la varience, etc, directement en utilisant R:</p>

︡ee0fe37b-d9dc-4f94-ac00-03f2ab5c7fbd︡{"html": "<p>Bon, je ne suis pas un statisticien et ne fait pas de la statistique de haut vol ici. On calcule la moyenne, la varience, etc, directement en utilisant R:</p>"}︡
︠bc7b7cbf-6bbe-45eb-9a51-4fca384ee481︠
%r

mean(x)
︡0b17529b-7e85-40f9-b019-575398da9c69︡{"stdout": "[1] 4.933333"}︡
︠4f68440f-8f5a-4eb4-98f7-ed74899942a4︠
%r 

var(x)
︡001aa78f-00b7-4bf1-a066-41f09abdbbfa︡{"stdout": "[1] 7.209524"}︡
︠7bd9d766-fafa-4bd1-94e5-2d3a403fc54d︠
%r

summary(x)
︡d0f08a57-4d65-4f78-a61c-6df4670cf08d︡{"stdout": "Min. 1st Qu.  Median    Mean 3rd Qu.    Max. \n  1.000   3.500   5.000   4.933   6.500   9.000"}︡
︠9f77e0dc-9f1b-4f4a-9b3c-7a06a906d8dci︠
%html
<p><span>On fait ci-dessous un test-T disant que la moyenne de la population est de 5:&nbsp;</span></p>

︡3721d720-4962-41e8-abd3-b3a7ad0d4ca3︡{"html": "<p><span>On fait ci-dessous un test-T disant que la moyenne de la population est de 5:&nbsp;</span></p>"}︡
︠e330aa3c-a24c-45d6-9bb7-693604f103d9︠
%r

results = t.test(x, mu=5)
results
︡3049b6c4-df59-4b46-a078-1e7ad572ba77︡{"stdout": "One Sample t-test\n\ndata:  x \nt = -0.0962, df = 14, p-value = 0.9248\nalternative hypothesis: true mean is not equal to 5 \n95 percent confidence interval:\n 3.446399 6.420268 \nsample estimates:\nmean of x \n 4.933333"}︡
︠fc55b723-c92d-4616-8000-f08ae0c53275︠
%r

results['statistic']
︡2badba07-5fee-4985-b661-aec50a838f19︡{"stdout": "$statistic\n          t \n-0.09616147"}︡
︠0dd1ba16-5a62-424c-910b-ea6119ae0e05︠
%r

results['p.value']
︡b48caf0d-4b06-4ac4-a2c9-bbf012ea90f4︡{"stdout": "$p.value\n[1] 0.9247553"}︡
︠285606cd-10b8-4f53-9d77-585b5be1e4bfi︠
%html
<h3>Int&eacute;grer R et Sage</h3>
<p>Nous avons vu ci-dessus qu'il &eacute;tait possible d'utiliser l'interpr&eacute;teur de R directement depuis une cellule de Sage. Il est &eacute;galement possible de faire interagir plus &eacute;troitement R et Sage:</p>

︡24231f23-2ef8-4639-9d28-a729bf0ffa1c︡{"html": "<h3>Int&eacute;grer R et Sage</h3>\n<p>Nous avons vu ci-dessus qu'il &eacute;tait possible d'utiliser l'interpr&eacute;teur de R directement depuis une cellule de Sage. Il est &eacute;galement possible de faire interagir plus &eacute;troitement R et Sage:</p>"}︡
︠3c9da583-c50b-4763-bc4b-49ea8606bbe2︠
x = r([1,3,5,6,4,9,9,1,1,6,7,8,4,5,5])
︡53a6eb53-dc41-4071-a6df-c52a57cc1f3c︡︡
︠ec5fa070-d601-4e14-9149-a4b2fdfc0e5d︠
r.mean(x)
︡1bf328f6-6dc8-465b-aa17-7646feafe274︡{"stdout": "[1] 4.933333"}︡
︠bca9acf4-0919-4bf9-8c69-0ac06219abe3︠
x.var()
︡c6f6999e-875d-4c1c-b390-defc442459b1︡{"stdout": "[1] 7.209524"}︡
︠70685d4f-074e-46f7-bdb2-6c7836a03fce︠
r.summary(x)
︡94b49882-d09f-4851-9abf-d0b5d54bf905︡{"stdout": "Min. 1st Qu.  Median    Mean 3rd Qu.    Max. \n  1.000   3.500   5.000   4.933   6.500   9.000"}︡
︠5cd4781d-2796-4684-81c3-7c56ac6469dai︠
%html
<p><span>On fait ci-dessous un test-T disant que la moyenne de la population est de 5:&nbsp;</span></p>

︡e7be06da-176f-4a46-97cc-8abed5f2258e︡{"html": "<p><span>On fait ci-dessous un test-T disant que la moyenne de la population est de 5:&nbsp;</span></p>"}︡
︠55bfd957-89ef-4a18-a0d1-bb937e24943c︠
results = r.t_test(x, mu=5)
results
︡e4cc4051-f43d-427e-8bbb-c3a387872814︡{"stdout": "One Sample t-test\n\ndata:  sage25 \nt = -0.0962, df = 14, p-value = 0.9248\nalternative hypothesis: true mean is not equal to 5 \n95 percent confidence interval:\n 3.446399 6.420268 \nsample estimates:\nmean of x \n 4.933333"}︡
︠1ccd364d-07f6-46fa-9910-e159bd0be57bi︠
%html
<p><span>On peut convertir le r&eacute;sultat du test en une structure de donn&eacute;es Python (un dictionnaire) pour en explorer l'infornation de mani&egrave;re plus flexible:</span></p>
<p>la variable testresult est maintenant un dictionnaire Python qu'il est possible d'utiliser comme n'importe quelle structure de donn&eacute;e de ce langage.</p>

︡298ac77c-b767-4e80-ac8d-8c00709ecfa6︡{"html": "<p><span>On peut convertir le r&eacute;sultat du test en une structure de donn&eacute;es Python (un dictionnaire) pour en explorer l'infornation de mani&egrave;re plus flexible:</span></p>\n<p>la variable testresult est maintenant un dictionnaire Python qu'il est possible d'utiliser comme n'importe quelle structure de donn&eacute;e de ce langage.</p>"}︡
︠7e5a72aa-8ded-4f2f-8fa5-0c2a0e965b3f︠
testresult = results._sage_()
testresult
︡8a23c21c-8b2e-44be-933e-91af022dd9f0︡{"stdout": "{'_r_class': 'htest', '_Names': ['statistic', 'parameter', 'p.value', 'conf.int', 'estimate', 'null.value', 'alternative', 'method', 'data.name'], 'DATA': {'p_value': 0.92475529872458795, 'alternative': 'two.sided', 'data_name': 'sage25', 'null_value': {'_Names': 'mean', 'DATA': 5}, 'conf_int': {'conf_level': 0.94999999999999996, 'DATA': [3.4463990798025899, 6.4202675868640702]}, 'statistic': {'_Names': 't', 'DATA': -0.096161467028551301}, 'estimate': {'_Names': 'mean of x', 'DATA': 4.93333333333333}, 'parameter': {'_Names': 'df', 'DATA': 14}, 'method': 'One Sample t-test'}}"}︡
︠ffa9c016-4637-44be-88cf-3a2bbe3ee2fa︠
print 'p-value is: ', testresult['DATA']['p_value']

print 'Statistic:   %s = %s' % (testresult['DATA']['statistic']['_Names'], testresult['DATA']['statistic']['DATA'])

print '%s%% C.I.:  %s' % (round(100*testresult['DATA']['conf_int']['conf_level']),testresult['DATA']['conf_int']['DATA'])
︡7a6e9c6e-65da-4f0c-9162-7bab9a4ba550︡{"stdout": "p-value is:  0.924755298725\nStatistic:   t = -0.0961614670286\n95.0% C.I.:  [3.4463990798025899, 6.4202675868640702]"}︡
︠36ef7277-d5d9-4610-99c3-93f1cffa5110i︠
%html
<p>Il est possible d'utiliser R pour la simulation et Python pour reste:</p>
<p><span>En R, rnorm(200, 5, 2)&nbsp;retourne un vecteur de 200 valeurs tir&eacute;es d'une distribution gaussienne de moyenne 5 et d'&eacute;cart-type 2.</span></p>

︡81148589-05e3-4017-ad02-0c133b974983︡{"html": "<p>Il est possible d'utiliser R pour la simulation et Python pour reste:</p>\n<p><span>En R, rnorm(200, 5, 2)&nbsp;retourne un vecteur de 200 valeurs tir&eacute;es d'une distribution gaussienne de moyenne 5 et d'&eacute;cart-type 2.</span></p>"}︡
︠7ee232ac-aa9d-46cf-8a5d-817b6d2a9968︠
r_norm = r.rnorm(200, 5, 2)
r_norm
︡25dc975b-8529-4567-b59d-5d5d73c2842f︡{"stdout": "[1]  2.932860130850042  7.019686630221097 -1.255683824281048  1.284682617292919  4.687406533398461\n  [6]  4.332701977430219  6.751191342748030  7.121697101716489  5.926438605074823  6.334666794362983\n [11]  4.635204545604861  4.776158262740585  9.773172896202670  6.206230354875271  5.524138794796816\n [16]  1.993938158183552  4.484702257245935  7.071366381243096  3.619284759614675  8.077753304874742\n [21]  6.218972984020207  5.247320252027164  2.516019119684823  4.033597052001843  4.958893571987001\n [26]  3.403126682206814  5.655490158917621  7.271838898286782  3.765106457817799  5.451735091594872\n [31]  4.176481608503499  7.877050770942954  2.307803569500903  7.569826944999753 10.223064206614417\n [36]  4.901327685507820  1.935225870705978  3.283511904922625  8.360971478418904  3.976855878787579\n [41]  5.521794389631239  4.581571444603891  4.467008550497113  4.778494557810026  7.692700581136485\n [46]  5.594038459986956  4.519053718942827  5.832301491611340  5.731401564383916  4.967714648649425\n [51]  1.811666211753191  7.904763247132804  5.693298633222565  2.768379816491648  8.478469517126006\n [56]  8.072145718690999  7.630006618541160  4.429098915431492  6.163584011128266  5.380622971635888\n [61]  7.100721326806628  4.966086824247173  8.130760593831180  4.815648504831355  1.416877381503559\n [66]  1.513040533976527  6.125241602775927  5.612790766660552  3.558414126656045  9.511485767492793\n [71]  5.851324157742610  5.548342694056521  5.134226798241416  5.266050559599313  4.777542568985157\n [76]  6.022256387936848  7.154804013798771  4.140771445791568  4.067403256376745  4.328932794482435\n [81]  1.823695047721291  8.064729826025323  4.055429211775283  5.236005540210176  3.471364825012341\n [86]  6.457393789264209  5.494824775152410  5.381001231764175  5.130996327173515  7.639242191276614\n [91] 10.468845614687545  2.375220080031704  4.673200549235761  8.753786085379827  5.087739130539235\n [96]  6.062131594853822  5.080768895902694  1.974211096686897  3.756011504462642  3.493085036373910\n[101]  4.743451506993039  5.706417100711520  7.387707487817293  6.839882171968608  6.362991001974821\n[106]  4.090167877166839  5.692063290995042  6.639155776705820  4.767580327585668  1.662674019718402\n[111]  5.068210944505697  7.521537181860581  1.915789731760151  2.831255966622024  6.586507291395144\n[116]  3.604936102650397  4.640749546432859  7.029542313314467  5.580951812457584  7.364958194718141\n[121]  4.612703264794814  5.411059149403209  2.181587108537966  4.002556772307646  3.071879648808284\n[126]  8.058382122859093  5.606853605478570  5.699699081021991  4.887637035308696  7.031255412229200\n[131]  5.428258790807652  5.858763520186419  4.250640895750221  0.648944394495954  5.159199420369405\n[136]  3.447743920927558  7.677861778304395  7.101381926107556  1.820231162733710  5.430085092929261\n[141]  3.341601603761124  5.429044915448719  9.281791436631117  6.352536931571318  5.342454436616697\n[146]  2.739368600097829  3.559195797131109  2.256956048268007  1.527824244197628  5.782138357492101\n[151]  8.736381337621978  3.968425946949193  3.917634206736804  6.117988411933440  4.286253894093873\n[156]  5.204720547740065  3.064516806581130  4.748036574912142  6.934419375508805  6.966185036662761\n[161]  5.946017440217970  4.976883109005738  5.783788335010193  5.706222430681183  3.513150539961214\n[166]  7.343510303916510  2.689026331281138  6.823415881985600  4.267755832702060  8.376560923943423\n[171]  7.948308133473203  3.004540163354743  2.933666812774234  7.543954036235458  4.955392781847880\n[176]  4.307077576940843  5.507427923278904  4.804893580967775  5.779556518636309  6.465924338187170\n[181]  7.910016166193128  8.749501642241245  9.126545434210815  3.237828819836731  3.572230427006931\n[186]  5.663311542622086  5.186940952932177  5.583700151939425  7.188544347397083  3.580407511612389\n[191]  3.629980964356285  3.034240838345398  4.680429657849403  3.303505437948030  6.591872452393569\n[196]  7.500828912379446  7.046754679519861  6.350760130949962  3.762547820866622  5.413958248299800"}︡
︠8a59b3f5-f9b9-40c7-b090-f3c042d42c45︠
r_norm.summary()
︡48938574-63d9-4ea2-bf52-52a97f8c0c82︡{"stdout": "Min.        1st Qu.         Median           Mean        3rd Qu.           Max. \n-1.25568382428  3.97474839583  5.25668540581  5.24372022367  6.58784858164 10.46884561470"}︡
︠31688318-8cbd-450b-8ef7-c2c0f1fff93ci︠
%html
<p>Il est possible de r&eacute;cup&eacute;rer les nombres al&eacute;atoires g&eacute;n&eacute;r&eacute;s dans une structure de donn&eacute;es python et, par exemple, de les trier avec les outils python pr&eacute;vus pour trier une liste</p>

︡536e7ca9-95a6-404b-bfdb-fa0a94d2850c︡{"html": "<p>Il est possible de r&eacute;cup&eacute;rer les nombres al&eacute;atoires g&eacute;n&eacute;r&eacute;s dans une structure de donn&eacute;es python et, par exemple, de les trier avec les outils python pr&eacute;vus pour trier une liste</p>"}︡
︠a8a7f343-b505-41c5-9f5d-efcf6709ed9f︠
sage_norm = sorted(r_norm._sage_())
sage_norm
︡e48d22d2-fe6a-4732-b4e3-0dae551f5196︡{"stdout": "[-1.25568382428105, 0.64894439449595398, 1.28468261729292, 1.41687738150356, 1.51304053397653, 1.52782424419763, 1.6626740197184, 1.8116662117531901, 1.82023116273371, 1.8236950477212901, 1.91578973176015, 1.93522587070598, 1.9742110966869, 1.99393815818355, 2.1815871085379701, 2.2569560482680102, 2.3078035695009, 2.3752200800317, 2.5160191196848198, 2.68902633128114, 2.7393686000978299, 2.7683798164916502, 2.8312559666220198, 2.9328601308500399, 2.9336668127742298, 3.0045401633547399, 3.0342408383453998, 3.0645168065811301, 3.07187964880828, 3.23782881983673, 3.2835119049226198, 3.3035054379480302, 3.34160160376112, 3.40312668220681, 3.44774392092756, 3.4713648250123401, 3.4930850363739099, 3.51315053996121, 3.5584141266560398, 3.5591957971311099, 3.5722304270069301, 3.5804075116123899, 3.6049361026504001, 3.6192847596146702, 3.6299809643562901, 3.7560115044626401, 3.7625478208666201, 3.7651064578178, 3.9176342067368002, 3.96842594694919, 3.9768558787875801, 4.0025567723076501, 4.0335970520018396, 4.0554292117752802, 4.0674032563767497, 4.0901678771668397, 4.1407714457915699, 4.1764816085034999, 4.2506408957502204, 4.2677558327020604, 4.2862538940938704, 4.3070775769408396, 4.3289327944824301, 4.3327019774302196, 4.4290989154314904, 4.46700855049711, 4.4847022572459396, 4.5190537189428301, 4.58157144460389, 4.6127032647948099, 4.6352045456048598, 4.6407495464328603, 4.6732005492357596, 4.6804296578494, 4.6874065333984598, 4.7434515069930399, 4.74803657491214, 4.7675803275856703, 4.7761582627405801, 4.7775425689851598, 4.7784945578100304, 4.8048935809677804, 4.8156485048313504, 4.8876370353086998, 4.9013276855078196, 4.9553927818478796, 4.9588935719869998, 4.9660868242471699, 4.9677146486494204, 4.9768831090057404, 5.0682109445057, 5.0807688959026898, 5.0877391305392301, 5.1309963271735102, 5.1342267982414196, 5.1591994203693998, 5.1869409529321802, 5.2047205477400604, 5.2360055402101802, 5.2473202520271602, 5.2660505595993099, 5.3424544366167002, 5.3806229716358898, 5.3810012317641798, 5.4110591494032096, 5.4139582482998003, 5.4282587908076501, 5.4290449154487197, 5.4300850929292599, 5.4517350915948697, 5.4948247751524102, 5.5074279232789003, 5.52179438963124, 5.5241387947968201, 5.5483426940565197, 5.5809518124575801, 5.5837001519394196, 5.5940384599869599, 5.6068536054785696, 5.6127907666605497, 5.6554901589176199, 5.6633115426220897, 5.6920632909950397, 5.6932986332225699, 5.6996990810219899, 5.70622243068118, 5.7064171007115201, 5.73140156438392, 5.7795565186363103, 5.7821383574920997, 5.7837883350102004, 5.8323014916113403, 5.8513241577426101, 5.8587635201864199, 5.9264386050748197, 5.94601744021797, 6.0222563879368503, 6.0621315948538204, 6.1179884119334398, 6.1252416027759304, 6.16358401112827, 6.2062303548752702, 6.2189729840202101, 6.3346667943629802, 6.3507601309499604, 6.3525369315713203, 6.3629910019748204, 6.4573937892642101, 6.4659243381871701, 6.5865072913951401, 6.5918724523935701, 6.6391557767058202, 6.7511913427480303, 6.8234158819855999, 6.8398821719686103, 6.9344193755088002, 6.9661850366627602, 7.0196866302211003, 7.0295423133144697, 7.0312554122291999, 7.0467546795198599, 7.0713663812430996, 7.1007213268066298, 7.1013819261075604, 7.1216971017164896, 7.1548040137987696, 7.1885443473970803, 7.2718388982867799, 7.3435103039165099, 7.3649581947181399, 7.3877074878173001, 7.5008289123794496, 7.5215371818605803, 7.5439540362354602, 7.5698269449997504, 7.6300066185411604, 7.63924219127661, 7.6778617783043996, 7.6927005811364904, 7.87705077094295, 7.9047632471328004, 7.9100161661931301, 7.9483081334732004, 8.0583821228591006, 8.0647298260253208, 8.0721457186910008, 8.0777533048747401, 8.1307605938311802, 8.3609714784189002, 8.3765609239434191, 8.4784695171259994, 8.7363813376219799, 8.7495016422412508, 8.7537860853798293, 9.1265454342108097, 9.2817914366311207, 9.5114857674928004, 9.77317289620267, 10.223064206614399, 10.468845614687501]"}︡
︠58fd173c-6ff8-4c9b-af87-dd2223912d08i︠
%html
<p>On peut ensuite utiliser les possibilit&eacute;s de visualisation de Sage:</p>

︡a05ceb5b-5f7e-4b9e-b194-be20f359c610︡{"html": "<p>On peut ensuite utiliser les possibilit&eacute;s de visualisation de Sage:</p>"}︡
︠17f0758d-6b19-4058-bd51-f25de3ad9bdd︠
list_plot(sage_norm, xmin=-2)
︡03286e48-4d1a-4e67-ac4f-9386f768434f︡{"html": "<font color='black'><img src='cell://sage0.png'></font>"}︡