编辑“︁
R语言
”︁
跳转到导航
跳转到搜索
脚本错误:没有“Mainspace editnotice”这个模块。
警告:
您没有登录。如果您进行任何编辑,您的IP地址会公开展示。如果您
登录
或
创建账号
,您的编辑会以您的用户名署名,此外还有其他益处。
反垃圾检查。
不要
加入这个!
{{noteTA |G1 = IT |G2 = FL }} {{Infobox programming language |name = R |logo = R logo.svg |logo size = 160px |paradigm = [[多重編程範式]]:[[阵列编程|阵列式]]、[[物件導向程式設計|物件導向]]、[[函數程式語言|函數式]]、[[反射式编程|反射式]]、[[指令式編程|指令式]]、<ref name="#1">{{Cite journal|title=Evaluating the Design of the R Language|url=https://link.springer.com/chapter/10.1007/978-3-642-31057-7_6|last=Morandat|first=Floréal|last2=Hill|first2=Brandon|date=2012|journal=ECOOP 2012 – Object-Oriented Programming|publisher=Springer|doi=10.1007/978-3-642-31057-7_6|editor-last=Noble|editor-first=James|series=Lecture Notes in Computer Science|location=Berlin, Heidelberg|pages=104–131|language=en|isbn=978-3-642-31057-7|last3=Osvald|first3=Leo|last4=Vitek|first4=Jan|access-date=2021-07-18|archive-date=2021-07-18|archive-url=https://web.archive.org/web/20210718234723/https://link.springer.com/chapter/10.1007/978-3-642-31057-7_6}}</ref>[[过程式編程|过程式]]<ref>{{Cite web|title=Programming Paradigms|url=https://homerhanumat.github.io/r-notes/programming-paradigms.html|access-date=2021-07-18|date=2021-01-31|last=White|first=Homer|publisher=Beginning Computer Science with R|language=美国英语|archive-date=2021-07-18|archive-url=https://web.archive.org/web/20210718233143/https://homerhanumat.github.io/r-notes/programming-paradigms.html}}</ref> |year = {{start date and age|1993|8}} |designer = [[罗斯·伊哈卡]]和[[罗伯特·杰特曼]] |developer = R核心開發小組 | latest release version = {{wikidata|property|reference|P348}} | latest release date = {{wikidata|qualifier|P348|P577}},{{Time ago|{{wikidata|qualifier|raw|P348|P577}}}} |typing = [[類型系統|动态类型]]<ref>{{Cite web|title=Why I don’t like Dynamic Typing|url=https://win-vector.com/2012/02/25/why-i-dont-like-dynamic-typing/|access-date=2021-07-18|date=2012-02-25|last=jmount|work=Win Vector LLC|language=en-US|archive-date=2021-07-17|archive-url=https://web.archive.org/web/20210717140856/https://win-vector.com/2012/02/25/why-i-dont-like-dynamic-typing/}}</ref> |implementations = |dialects = |influenced_by = [[S語言|S]]、[[Scheme]]、[[Lisp]] | influenced = [[Julia (编程语言)|Julia]]<ref name="Introduction">{{cite web |url=https://docs.julialang.org/en/stable/manual/introduction/#man-introduction-1 |title=Introduction |work=The Julia Manual |access-date=2018-08-05 |archive-url=https://web.archive.org/web/20180620172516/https://docs.julialang.org/en/stable/manual/introduction/#man-introduction-1#man-introduction-1 |archive-date=20 June 2018 }}</ref> | website = {{Official URL}} | wikibooks = R Programming | file_ext = {{Unbulleted list|.r<ref>{{Cite web|title=R scripts|url=http://mercury.webster.edu/aleshunas/R_learning_infrastructure/R%20scripts.html|access-date=2021-07-17|website=mercury.webster.edu|archive-date=2022-01-15|archive-url=https://web.archive.org/web/20220115062658/http://mercury.webster.edu/aleshunas/R_learning_infrastructure/R%20scripts.html}}</ref>|.rdata|.rds|.rda<ref>{{Cite web|title=R Data Format Family (.rdata, .rda)|url=https://www.loc.gov/preservation/digital/formats/fdd/fdd000470.shtml|access-date=2021-07-18|date=2017-06-09|work=www.loc.gov|archive-date=2021-07-17|archive-url=https://web.archive.org/web/20210717145243/https://www.loc.gov/preservation/digital/formats/fdd/fdd000470.shtml}}</ref>}} }} '''R语言是'''一種[[自由軟體]][[程式語言]]與操作環境,主要用于[[统计]]分析、绘图以及[[数据挖掘]]。R由[[新西蘭]][[奧克蘭大學]]的统计学家[[罗斯·伊哈卡]]和[[罗伯特·杰特曼]]開發,現在由R核心小组負責開發,同时也有其他用户编写了诸多外挂的[[软件包]]。R以[[S語言|S语言]]为基础,其词法作用域语义來自[[Scheme]]。R的后台程序大多由[[C语言]]、[[Fortran|FORTRAN语言]]和R自己写成。<ref name=":02">{{Cite web|title=R FAQ|url=https://cran.r-project.org/doc/FAQ/R-FAQ.html|access-date=2021-07-18|work=cran.r-project.org|archive-date=2011-07-09|archive-url=https://web.archive.org/web/20110709085359/http://cran.r-project.org/doc/FAQ/R-FAQ.html}}</ref> R语言是[[GNU計劃]]的一个项目,所以其[[原始碼]]可自由下載使用。R也有已編譯的[[執行檔]]版本可以下載,可在多种平台下运行,包括[[UNIX]](也包括[[FreeBSD]]和[[Linux]])、[[Microsoft Windows|Windows]]和[[MacOS]]。R可以以[[命令行界面|命令行]]操作,同時有人開發了幾種[[圖形用戶界面]],其中包括[[RStudio]]<ref name=":02" />与[[Jupyter]]。<ref>{{Cite web|title=Using the R programming language in Jupyter Notebook — Anaconda documentation|url=https://docs.anaconda.com/anaconda/navigator/tutorials/r-lang/|access-date=2021-12-29|work=docs.anaconda.com|archive-date=2022-04-26|archive-url=https://web.archive.org/web/20220426094745/https://docs.anaconda.com/anaconda/navigator/tutorials/r-lang/}}</ref> 在[[TIOBE指數|TIOBE]]2022年1月对编程语言人气的排名中,R排名第12。<ref>{{Cite web|title=index {{!}} TIOBE - The Software Quality Company|url=https://www.tiobe.com/tiobe-index/|access-date=2021-07-18|work=www.tiobe.com|archive-date=2018-02-25|archive-url=https://web.archive.org/web/20180225101948/https://www.tiobe.com/tiobe-index/}}</ref> == 发展历程 == R语言以S语言为基础,增加了Scheme语言中[[词法作用域]]这一机制,使程序员得以将代码中某一对象的适用范围限制到一小段代码之中。<ref name="#1"/>S由里克·贝克尔、{{le|约翰·钱伯斯|John Chambers (statistician)}}、道格·邓恩、琼·麦克雷、以及朱迪·席林于1976年前后于[[贝尔实验室]]发明。<ref>{{Cite journal|title=A Brief History of S|url=https://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.131.1428&rank=6&q=a%20brief%20history%20of%20S&osm=&ossid=|last=Becker|first=Richard A.|journal=|access-date=2022-01-03|archive-date=2022-01-03|archive-url=https://web.archive.org/web/20220103191539/https://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.131.1428&rank=6&q=a%20brief%20history%20of%20S&osm=&ossid=}}</ref>S是一种用于数据分析的[[直譯語言|解释型语言]],无需[[編譯器|编译器]]即可运行。通常用S语言编写的代码都可以不作修改地在R环境下运行。<ref>{{Cite web|title=R: What is R?|url=https://www.r-project.org/about.html|access-date=2021-07-18|work=www.r-project.org|archive-date=2011-07-19|archive-url=https://web.archive.org/web/20110719150708/http://www.r-project.org/about.html}}</ref>Scheme是[[LISP|Lisp]]语言的一个分支,<ref>{{Cite journal|title=S, R, and Data Science|author=John M. Chambers|journal=The R Journal|issue=1|year=2020|volume=11|page=462-476}}</ref>由[[傑拉德·傑伊·薩斯曼|杰拉尔德·J·萨斯曼]]和[[小蓋伊·史提爾|小盖伊·L·斯蒂尔]]于1975年前后在[[麻省理工学院]]发明。<ref>{{Cite journal |last=Sussman |first=Gerald Jay |last2=Steele |first2=Guy L. |title=The First Report on Scheme Revisited |url=https://link.springer.com/article/10.1023/A:1010079421970 |journal=Higher-Order and Symbolic Computation |language=en |date=1998-12-01 |volume=11 |issue=4 |page=399-404 |doi=10.1023/A:1010079421970 |issn=1573-0557 |access-date=2023-11-14 |archive-date=2025-01-14 |archive-url=https://web.archive.org/web/20250114081603/https://link.springer.com/article/10.1023/A:1010079421970 |dead-url=no }}</ref> 1991年,新西兰奥克兰大学的统计学家罗斯·伊哈卡和罗伯特·杰特曼开始对S语言的一个新版本进行开发。<ref>{{Cite web|title=Academic unfazed by rock star status|url=https://www.nzherald.co.nz/nz/academic-unfazed-by-rock-star-status/LMU5EIMP7QCMZFCBM3UNW4C7CI/|access-date=2022-01-03|work=NZ Herald|language=en-NZ|archive-date=2022-04-11|archive-url=https://web.archive.org/web/20220411070037/https://www.nzherald.co.nz/nz/academic-unfazed-by-rock-star-status/LMU5EIMP7QCMZFCBM3UNW4C7CI/}}</ref>伊哈卡与杰特曼两人名字首字母都是R,R语言因此得名。同时,R这个单一字母的名字也表明R语言与S语言一脉相承。<ref name=":02" />1993年8月,伊哈卡与杰特曼在数据平台StatLib和邮件列表''s-news''中发布了R的早期版本。1995年,在统计学家马丁·梅克勒的建议下,伊哈卡与杰特曼通过[[GNU通用公共许可证]]把R做成了一款免费开源软件。软件于1995年6月进行了首次官方发布。<ref>{{Cite web|title=R : Past and Future History -- A Free Software Project|url=https://cran.r-project.org/doc/html/interface98-paper/paper_2.html|access-date=2022-01-03|work=cran.r-project.org|archive-date=2017-07-11|archive-url=https://web.archive.org/web/20170711130614/http://cran.r-project.org/doc/html/interface98-paper/paper_2.html}}</ref>首个稳定测试版本(1.0)于2000年2月29日发布。<ref>{{Cite web|title=The R Project: A Brief History and Thoughts About the Future|url=https://www.stat.auckland.ac.nz/~ihaka/downloads/Massey.pdf|access-date=2022-01-03|author=Ross Ihaka|format=pdf|publisher=Ross Ihaka|language=en|archive-date=2018-02-02|archive-url=https://web.archive.org/web/20180202005328/https://www.stat.auckland.ac.nz/%7Eihaka/downloads/Massey.pdf}}</ref> [[CRAN|R综合档案网]](Comprehensive R Archive Network;CRAN)于1997年4月23日正式上线。CRAN除了收藏了R的執行檔下載版、原始碼和說明文件,也收錄了各種用戶撰寫的[[R包|R軟件包]]。CRAN最早有3个[[镜像站|镜像]]以及12个软件包。<ref>{{Cite web|title=ANNOUNCE: CRAN|url=https://stat.ethz.ch/pipermail/r-announce/1997/000001.html|access-date=2022-01-03|work=stat.ethz.ch|archive-date=2021-03-08|archive-url=https://web.archive.org/web/20210308080935/https://stat.ethz.ch/pipermail/r-announce/1997/000001.html}}</ref>截止2022年1月,CRAN有101個鏡像站<ref>{{Cite web|title=CRAN - Mirrors|url=https://cran.r-project.org/mirrors.html|access-date=2022-01-15|work=cran.r-project.org|archive-date=2022-05-10|archive-url=https://web.archive.org/web/20220510151007/https://cran.r-project.org/mirrors.html}}</ref>以及18728个软件包。<ref>{{Cite web|title=CRAN - Contributed Packages|url=https://cran.r-project.org/web/packages/index.html|access-date=2022-01-03|work=cran.r-project.org|archive-date=2022-04-11|archive-url=https://web.archive.org/web/20220411070133/https://cran.r-project.org/web/packages/index.html}}</ref> 同样在1997年,R核心小组正式成立,以进一步对R语言进行开发。截止2022年1月,小组成员包括伊哈卡、杰特曼、钱伯斯以及梅克勒,同时也包括了统计学家{{le|库尔特·奥尔尼克|Kurt Hornik}}、道格拉斯·贝茨、{{le|彼得·达尔高|Peter Dalgaard}}、{{le|卢克·蒂尔尼|Luke Tierney}}、弗里德里希·莱施、{{le|托马斯·拉姆利|Thomas Lumley}}、邓肯·坦普尔·朗、迈克尔·劳伦斯、乌韦·利格斯、布莱恩·里普利、塞巴斯蒂安·迈耶、保罗·默雷尔、马丁·普卢默、迪伊潘·萨卡尔、西蒙·乌尔巴内克以及计算机科学家托马斯·卡利贝拉。<ref name=":02" />小组过去的成员包括塞思·福尔肯、圭多·马萨罗托、邓肯·默多克、马丁·摩根、海纳·施瓦特以及斯特凡诺·雅各斯。<ref>{{Cite web|title=R: Contributors|url=https://www.r-project.org/contributors.html|access-date=2022-01-03|work=www.r-project.org|archive-date=2022-04-23|archive-url=https://web.archive.org/web/20220423192906/https://www.r-project.org/contributors.html}}</ref>2003年4月<ref>{{Cite journal|title=R Foundation News|author=Martin Mächler|url=https://journal.r-project.org/archive/2014-2/r-foundation.pdf|journal=The R Journal|issue=2|others=Kurt Hornik|year=2014|volume=6|page=190-191|access-date=2022-01-03|archive-date=2022-01-20|archive-url=https://web.archive.org/web/20220120104641/https://journal.r-project.org/archive/2014-2/r-foundation.pdf}}</ref>,一个名为R基金会的非盈利组织正式成立,为的是更好地对R语言的开发提供支持。<ref name=":02" /> == 内置功能 == R主要用于数据分析。在R语言中,用于信息存储的[[数据结构]]包括[[向量]]、[[数组]]、[[串列 (抽象資料型別)|列表]]以及数据框。向量指一组带有固定顺序, [[資料類型|数据类型]]唯一的字串或数值,其内容可以填写到一维或多维的数组之中。二维数组也叫做[[矩阵]]。<ref name=":0">{{Cite book |url=https://link.springer.com/10.1007/978-0-387-79054-1_1 |title=Introductory Statistics with R |last=Dalgaard |first=Peter |date=2008 |publisher=Springer New York |location=New York, NY |isbn=978-0-387-79053-4 |pages=1–29 |language=en |chapter=Basics |doi=10.1007/978-0-387-79054-1_1}}</ref>R支持各种数组运算,与自由软件[[GNU Octave]]和商業軟件[[MATLAB]]的功能有所重叠。<ref>{{cite web |title=Benchmark 2 |url=http://www.sciviews.org/benchmark |website=SciViews |url-status=dead |archive-url=https://web.archive.org/web/20090808220014/http://www.sciviews.org/benchmark/ |archive-date=2009-08-08 |accessdate=2007-11-03}}</ref><ref>{{Cite web|title=A Comparison Study on Execution Performance of MATLAB and APL|url=http://www.sable.mcgill.ca/~hanfeng.c/eli/res/Comparison.pdf|access-date=March 9, 2022|author=Han-feng Chen, Wai-Mee Ching, and Da Zheng|format=PDF|publisher=McGill University|language=EN|archive-date=2022-03-03|archive-url=https://web.archive.org/web/20220303124342/http://www.sable.mcgill.ca/~hanfeng.c/eli/res/Comparison.pdf}}</ref>列表指一组数据类型可能有所不同的对象。一个字串向量与数值向量合在一起就可以成为一个列表。数据框本质上是一个列表,里面包含了一个或多个长度相同的向量。数据框将这些向量合并成表格,每一纵列都有一个单一的名称。<ref name=":0" />[[标量 (数学)|标量]]这一数据类型在R语言中并不存在,所谓的标量就是一个长度为一的向量。<ref>{{Cite web|title=Data structures · Advanced R.|url=http://adv-r.had.co.nz/Data-structures.html|access-date=2022-03-09|work=adv-r.had.co.nz|archive-date=2016-09-20|archive-url=https://web.archive.org/web/20160920225044/http://adv-r.had.co.nz/Data-structures.html}}</ref> 用户可以用R来进行一些基本的统计检验,构建线性及非线性的模型,对[[時間序列|时间序列]]加以分析,或对数据进行分类与[[聚类分析]]。R的另一強項是繪圖功能,画出的图表能够达到专业出版物的要求,也可加入數學符號。计算强度较大时,用户可在程序中嵌入C、[[C++]]以及FORTRAN语言以帮助运算。<ref>{{Cite web|title=R: What is R?|url=https://www.r-project.org/about.html|access-date=2022-03-09|work=www.r-project.org|archive-date=2011-07-19|archive-url=https://web.archive.org/web/20110719150708/http://www.r-project.org/about.html}}</ref> 因為S的血緣,R比其他統計學或數學專用的編程語言有更強的[[物件導向]](面向对象程序设计, S3, S4等)功能。 == 套件 == R的功能能夠透過由用戶撰寫的套件(Packages)增強。增加的功能有特殊的統計技術、繪圖功能,以及編程介面和數據輸出/輸入功能。這些軟件包是由R語言、[[LaTeX]]、[[Java]]及最常用[[C語言]]和[[Fortran]]撰寫。下載的執行檔版本會連同一批核心功能的軟件包,而根據CRAN紀錄有一萬多種不同的軟件包。其中有幾款較為常用,例如用於[[計量經濟學|經濟計量]]、[[財經分析]]、[[人文科學]]與社會科學研究以及[[人工智能]]。<ref>{{cite web |title=CRAN Task Views |url=http://cran.r-project.org/src/contrib/Views/ |website=The Comprehensive R Archive Network |url-status=live |archive-url=https://web.archive.org/web/20070625000503/http://www.cran.r-project.org/src/contrib/Views/ |archive-date=2007-06-25 |accessdate=2007-06-04}}</ref> == 發展 == [[生物信息学]]社群時常使用R進行分子生物學數據分析。[[Bioconductor]]計劃就是讓R作為基因圖譜分析工具。 [[Gnumeric]]開發者正和R開發者合作,改善Gnumeric計算結果的精確度。<ref>{{cite web|title=Gnumeric 1.4 is Here!|url=http://gnomedesktop.org/node/2090|accessdate=2006-04-30|date=2004年12月19日|last=Gnumeric|first=Team|publisher=The [[GNOME]] Project|archiveurl=https://web.archive.org/web/20061005023929/http://gnomedesktop.org/node/2090|archivedate=2006年10月5日|deadurl=yes}}</ref> == R新聞雜誌 == 《R新聞雜誌》(''R Newsletter'')每年會出版兩至三次,為一份免費的電子雜誌,內容有關統計學軟件發展及R語言開發資訊。第一期在2001年一月出版。从2008年开始,被R Journal替代。<ref>{{Cite web |title=CRAN: R News |url=http://cran.r-project.org/doc/Rnews/ |website=The Comprehensive R Archive Network |url-status=live |archive-url=https://web.archive.org/web/20070528211443/http://cran.r-project.org/doc/Rnews/ |archive-date=2007-05-28 |accessdate=2007-06-04}}</ref> == 例子 == === 基本语法 === 下面的例子展示基本的语言语法和命令行界面使用。在R语言中,一般偏好采用两字符合成箭头的[[赋值语句|赋值算符]]<code><-</code>,但是在某些情况也可以使用<code>=</code><ref>{{cite web|author=R Development Core Team|title=Assignments with the = Operator|url=https://developer.r-project.org/equalAssign.html|access-date=2018-09-11|archive-date=2019-01-08|archive-url=https://web.archive.org/web/20190108050445/http://developer.r-project.org/equalAssign.html|dead-url=no}}</ref>{{refn | most used assignment operator in R is <code><-</code> * {{cite web|title=Writing R Extensions|url=https://cran.r-project.org/doc/manuals/R-exts.html#Tidying-R-code|access-date=2018-09-11|author=R Development Core Team|quote=[...] we recommend the consistent use of the preferred assignment operator ‘<-’ (rather than ‘=’) for assignment.|archive-date=2019-01-06|archive-url=https://web.archive.org/web/20190106160844/https://cran.r-project.org/doc/manuals/R-exts.html#Tidying-R-code|dead-url=no}} * {{cite web|title=Google's R Style Guide|url=https://google.github.io/styleguide/Rguide.xml#assignment|access-date=2018-09-11|archive-date=2019-01-11|archive-url=https://web.archive.org/web/20190111060407/https://google.github.io/styleguide/Rguide.xml#assignment|dead-url=no}} * {{cite web|last=Wickham|first=Hadley|title=Style Guide|url=http://stat405.had.co.nz/r-style.html|access-date=2018-09-11|archive-date=2019-01-07|archive-url=https://web.archive.org/web/20190107005006/http://stat405.had.co.nz/r-style.html|dead-url=no}} * {{cite web|last=Bengtsson|first=Henrik|title=R Coding Conventions (RCC) – a draft|url=https://docs.google.com/document/preview?id=1esDVxyWvH8AsX-VJa-8oqWaHLs4stGlIbk8kLc5VlII&pli=1|access-date=2018-09-11|date=January 2009|archive-date=2016-03-19|archive-url=https://web.archive.org/web/20160319131206/https://docs.google.com/document/preview?id=1esDVxyWvH8AsX-VJa-8oqWaHLs4stGlIbk8kLc5VlII&pli=1|dead-url=no}} }}。 <syntaxhighlight lang="rout"> > x <- 1:6 # 在当前环境中创建数值向量。 > y <- x^2 # 基于在x中的值创建向量。 > print(y) # 打印向量的内容。 [1] 1 4 9 16 25 36 > z <- x + y # 创建是为x与y的和的新向量。 > z # 返回z的内容至当前环境。 [1] 2 6 12 20 30 42 > z_matrix <- matrix(z, nrow=3) # 创建将向量z转变为3x2矩阵对象的新矩阵。 > z_matrix [,1] [,2] [1,] 2 20 [2,] 6 30 [3,] 12 42 > 2*t(z_matrix)-2 # 转置这个矩阵,对每个元素乘以2,从矩阵中每个元素减去2,并返回结果至终端。 [,1] [,2] [,3] [1,] 2 10 22 [2,] 38 58 82 > new_df <- data.frame(t(z_matrix), row.names=c('A','B')) # 创建包含来自转置的z_matrix的数据的新data.frame对象,具有横行名字'A'和'B'。 > names(new_df) <- c('X','Y','Z') # 设置new_df的纵列名字为X、Y和Z。 > print(new_df) # 打印当前结果。 X Y Z A 2 6 12 B 20 30 42 > new_df$Z # 输出Z列。 [1] 12 42 > new_df$Z==new_df['Z'] && new_df[3]==new_df$Z # data.frame的Z列可以使用$Z、['Z']或[3]语法来访问,得到的值是相同的。 [1] TRUE > attributes(new_df) # 打印关于new_df对象的特性信息。 $names [1] "X" "Y" "Z" $row.names [1] "A" "B" $class [1] "data.frame" > attributes(new_df)$row.names <- c('one','two') # 访问并接着变更row.names特性;还可以使用rownames()来完成。 > new_df X Y Z one 2 6 12 two 20 30 42 </syntaxhighlight> === 函数的结构 === R语言的力量之一是易于创建新函数。在函数体内的对象保持局部于这个函数,而且可以返回任何数据类型<ref>{{cite web|url=http://www.statmethods.net/management/userfunctions.html|title=Quick-R: User-Defined Functions|first=Robert|last=Kabacoff|year=2012|access-date=2018-09-28|website=statmethods.net|archive-date=2019-01-07|archive-url=https://web.archive.org/web/20190107165538/https://www.statmethods.net/management/userfunctions.html|dead-url=no}}</ref>。例如: <syntaxhighlight lang="r"> # 声明函数“f”具有参数“x”、“y“。 # 它返回x和y的线性组合。 f <- function(x, y) { z <- 3 * x + 4 * y return(z) ## 这里的return()函数是可选的。 } </syntaxhighlight> <syntaxhighlight lang="rout"> > f(1, 2) [1] 11 > f(c(1,2,3), c(5,3,4)) [1] 23 18 25 > f(1:3, 4) [1] 19 22 25 </syntaxhighlight> === 建模和绘图 === R语言对数据建模和图形有内建支持。下列例子展示R语言如何轻易的生成并绘制带有[[残差]]的[[线性回归]]模型。 [[File:Plots from lm example.svg|right|thumb|400px|出自绘制“模型” (例子中“plot.lm()”函数)的诊断图。]] <syntaxhighlight lang="rout"> > x <- 1:6 # 创建x和y值。 > y <- x^2 > model <- lm(y ~ x) # 线性回归模型y = A + B * x。 > summary(model) # 显示这个模型的深入总结。 Call: lm(formula = y ~ x) Residuals: 1 2 3 4 5 6 7 8 9 10 3.3333 -0.6667 -2.6667 -2.6667 -0.6667 3.3333 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) -9.3333 2.8441 -3.282 0.030453 * x 7.0000 0.7303 9.585 0.000662 *** --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 3.055 on 4 degrees of freedom Multiple R-squared: 0.9583, Adjusted R-squared: 0.9478 F-statistic: 91.88 on 1 and 4 DF, p-value: 0.000662 > par(mfrow = c(2, 2)) # 创建2乘2格局的图表。 > plot(model) # 输出这个模型的诊断图。 </syntaxhighlight> === 曼德博集合 === 通过对方程''z'' = ''z''<sup>2</sup> + ''c''的前20次迭代计算[[曼德博集合]]的简短R代码,它针对不同的复数常量''c''而绘图。这个例子展示了: [[File:Mandelbrot Creation Animation.gif|right|thumb|400px|"Mandelbrot.gif" – R语言用14行代码创建的图形。]] * 使用社区开发库(叫做[[R包]]),这里是caTools包; * 处理[[复数 (数学)|复数]]; * 多维数值数组作为基本数据类型,用于变量<code>C</code>、<code>Z</code>和<code>X</code>。 <syntaxhighlight lang="r"> install.packages("caTools") # 安装外部包。 library(caTools) # 这个外部包提供write.gif函数。 jet.colors <- colorRampPalette(c("green", "pink", "#007FFF", "cyan", "#7FFF7F", "white", "#FF7F00", "red", "#7F0000")) dx <- 1500 # 定义宽度。 dy <- 1400 # 定义高度。 C <- complex(real = rep(seq(-2.2, 1.0, length.out = dx), each = dy), imag = rep(seq(-1.2, 1.2, length.out = dy), dx)) C <- matrix(C, dy, dx) # 重制形状为复数的方块矩阵。 Z <- 0 # 初始化Z为零。 X <- array(0, c(dy, dx, 20)) # 初始化输出3D数组。 for (k in 1:20) { # 循环具有20次迭代。 Z <- Z^2 + C # 中心差分方程。 X[, , k] <- exp(-abs(Z)) # 捕获结果。 } write.gif(X, "Mandelbrot.gif", col = jet.colors, delay = 100) </syntaxhighlight> == 参考文献 == {{Reflist|30em}} == 外部链接 == {{Commons category|R (programming language)}} * [http://www.r-project.org/ R語言官方主頁] {{Wayback|url=http://www.r-project.org/ |date=20110305165926 }} == 参见 == {{Portal box|计算机程序设计|统计学}} * [[RStudio]] - R语言的[[集成开发环境]](IDE) * [[SPSS]] - 另一種統計分析軟件 * [[Stata]] - 另一種統計分析軟件 * [[SAS系統]] - 另一種統計分析軟件 * [[S-PLUS]] - 同樣建基於S語言的統計分析軟件 {{-}} {{数值分析软件}} {{统计分析软件}} {{程序设计语言|BASIC}} {{GNU}} {{Authority control}} [[Category:程序设计语言]] [[Category:陣列編程語言]] [[Category:统计软件]] [[Category:统计编程语言]] [[Category:GNU计划软件]] [[Category:R語言]] [[Category:自由圖表軟件]] [[Category:1993年建立的程式語言]]
摘要:
请注意,所有对Local Chinese Wikipedia的贡献均可能会被其他贡献者编辑、修改或删除。如果您不希望您的文字作品被随意编辑,请不要在此提交。
您同时也向我们承诺,您提交的内容为您自己所创作,或是复制自公共领域或类似自由来源(详情请见
Project:著作权
)。
未经许可,请勿提交受著作权保护的作品!
取消
编辑帮助
(在新窗口中打开)
导航菜单
个人工具
未登录
讨论
贡献
创建账号
登录
命名空间
页面
讨论
大陆简体
不转换
简体
繁體
大陆简体
香港繁體
澳門繁體
大马简体
新加坡简体
臺灣正體
查看
阅读
编辑
查看历史
更多
搜索
导航
首页
最近更改
随机页面
MediaWiki帮助
工具
链入页面
相关更改
特殊页面
页面信息