I installed with apt-get, checked out the man page and added . /usr/share/autojump/autojump.sh to my .bashrc, like it says. When I cd around the filesystem, nothing gets added to ~/.local/share/autojump. I then tried adding . /usr/share/autojump/autojump.bash, but that didn't work either.
---
You'll probably want to add the following to your .bashrc:
source /usr/share/autojump/autojump.bash
©
August 28, 2014
How To Set Up an NFS Mount on Ubuntu 12.04
Author: Etel Sverdlov • Published: Sep 17, 2012 • Updated: Jun 11, 2014
About NFS (Network File System) Mounts
NFS mounts work to share a directory between several virtual servers. This has the advantage of saving disk space, as the home directory is only kept on one virtual private server, and others can connect to it over the network. When setting up mounts, NFS is most effective for permanent fixtures that should always be accessible.Setup
An NFS mount is set up between at least two virtual servers. The machine hosting the shared network is called the server, while the ones that connect to it are called ‘clients’.This tutorial requires 2 servers: one acting as the server and one as the client. We will set up the server machine first, followed by the client. The following IP addresses will refer to each one:
Master: 12.34.56.789
Client: 12.33.44.555
The system should be set up as root. You can access the root user by typing
sudo su-
Setting Up the NFS Server
Step One—Download the Required Software
Start off by using apt-get to install the nfs programs.apt-get install nfs-kernel-server portmap
Step Two—Export the Shared Directory
The next step is to decide which directory we want to share with the client server. The chosen directory should then be added to the /etc/exports file, which specifies both the directory to be shared and the details of how it is shared.Suppose we wanted to share two directories: /home and /var/nfs.
Because the /var/nfs/ does not exist, we need to do two things before we can export it.
First, we need to create the directory itself:
mkdir /var/nfs/Second, we should change the ownership of the directory to the user, nobody and the group, no group. These represent the default user through which clients can access a directory shared through NFS.
Go ahead and chown the directory:
chown nobody:nogroup /var/nfsAfter completing those steps, it’s time to export the directories to the other VPS:
nano /etc/exportsAdd the following lines to the bottom of the file, sharing both directories with the client:
/home 12.33.44.555(rw,sync,no_root_squash,no_subtree_check) /var/nfs 12.33.44.555(rw,sync,no_subtree_check)These settings accomplish several tasks:
- rw: This option allows the client server to both read and write within the shared directory
- sync: Sync confirms requests to the shared directory only once the changes have been committed.
- no_subtree_check: This option prevents the subtree checking. When a shared directory is the subdirectory of a larger filesystem, nfs performs scans of every directory above it, in order to verify its permissions and details. Disabling the subtree check may increase the reliability of NFS, but reduce security.
- no_root_squash: This phrase allows root to connect to the designated directory
exportfs -a
Setting Up the NFS Client
Step One—Download the Required Software
Start off by using apt-get to install the nfs programs.apt-get install nfs-common portmap
Step Two—Mount the Directories
Once the programs have been downloaded to the the client server, create the directories that will contain the NFS shared filesmkdir -p /mnt/nfs/home mkdir -p /mnt/nfs/var/nfsThen go ahead and mount them
mount 12.34.56.789:/home /mnt/nfs/home mount 12.34.56.789:/var/nfs /mnt/nfs/var/nfsYou can use the df -h command to check that the directories have been mounted. You will see them last on the list.
df -h
Filesystem Size Used Avail Use% Mounted on /dev/sda 20G 948M 19G 5% / udev 119M 4.0K 119M 1% /dev tmpfs 49M 208K 49M 1% /run none 5.0M 0 5.0M 0% /run/lock none 122M 0 122M 0% /run/shm 12.34.56.789:/home 20G 948M 19G 5% /mnt/nfs/home 12.34.56.789:/var/nfs 20G 948M 19G 5% /mnt/nfs/var/nfsAdditionally, use the mount command to see the entire list of mounted file systems.
mountYour list should look something like this:
/dev/sda on / type ext4 (rw,errors=remount-ro,barrier=0) [DOROOT] proc on /proc type proc (rw,noexec,nosuid,nodev) sysfs on /sys type sysfs (rw,noexec,nosuid,nodev) none on /sys/fs/fuse/connections type fusectl (rw) none on /sys/kernel/debug type debugfs (rw) none on /sys/kernel/security type securityfs (rw) udev on /dev type devtmpfs (rw,mode=0755) devpts on /dev/pts type devpts (rw,noexec,nosuid,gid=5,mode=0620) tmpfs on /run type tmpfs (rw,noexec,nosuid,size=10%,mode=0755) none on /run/lock type tmpfs (rw,noexec,nosuid,nodev,size=5242880) none on /run/shm type tmpfs (rw,nosuid,nodev) rpc_pipefs on /run/rpc_pipefs type rpc_pipefs (rw) 12.34.56.789:/home on /mnt/nfs/home type nfs (rw,vers=4,addr= 12.34.56.789,clientaddr=12.33.44.555) 12.34.56.789:/var/nfs on /mnt/nfs/var/nfs type nfs (rw,vers=4,addr=12.34.56.78,clientaddr=12.33.44.555)
Testing the NFS Mount
Once you have successfully mounted your NFS directories, you can test that they work by creating files on the Client and checking their availability on the Server.Create a file in each directory to try it out:
touch /mnt/nfs/home/example /mnt/nfs/var/nfs/exampleYou should then be able to find the files on the Server in the /home and /var/nfs directories.
ls /home
ls /var/nfs/You can ensure that the mount is always active by adding the directories to the fstab file on the client. This will ensure that the mounts start up after the server reboots.
nano /etc/fstab
12.34.56.789:/home /mnt/nfs/home nfs auto,noatime,nolock,bg,nfsvers=3,intr,tcp,actimeo=1800 0 0 12.34.56.789:/var/nfs /mnt/nfs/var/nfs nfs auto,noatime,nolock,bg,nfsvers=3,intr,tcp,actimeo=1800 0 0You can learn more about the fstab options by typing in:
man nfsAny subsequent restarts will include the NFS mount—although the mount may take a minute to load after the reboot
You can check the mounted directories with the two earlier commands:
df -h
mount
Removing the NFS Mount
Should you decide to remove a directory, you can unmount it using the umount command:cd sudo umount /directory nameYou can see that the mounts were removed by then looking at the filesystem again.
df -hYou should find your selected mounted directory gone.
How do I disable X at boot time so that the system boots in text mode?
For Ubuntu 11.10 and higher
Edit /etc/default/grub with your favorite editor,
Find out this line:
Change it to:
Update Grub:
No need to remove / disable
You will still be able to use X by typing
©
Edit /etc/default/grub with your favorite editor,
sudo nano /etc/default/grub
Find out this line:
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"
Change it to:
GRUB_CMDLINE_LINUX_DEFAULT="text"
Update Grub:
sudo update-grub
No need to remove / disable
lightdm
upstart conf, it already does that for you. lightdm.conf
# Check kernel command-line for inhibitors, unless we are being called
# manually
for ARG in $(cat /proc/cmdline); do
if [ "$ARG" = "text" ]; then
plymouth quit || :
stop
exit 0
fi
done
You will still be able to use X by typing
startx
after you logged in. August 20, 2014
Getting Started With R: Some Resources
There are many, many excellent resources for newcomers to R. Here are a few I can personally recommend.
Books
©
On-Line Introductions and Tutorials
- An Introduction to R: In R, run help.start(), then click on "An Introduction to R".
- An introduction to many topics. Not very tutorial, however. By Venables, Smith, et al. Also available at http://cran.r-project.org/doc/manuals/R-intro.pdf.
- Using R for Data Analysis and Graphics: http://cran.r-project.org/doc/contrib/usingR.pdf
- An easy-to-read tutorial. By J. H. Maindonald.
- Quick-R for SAS/SPSS/Stata Users: http://www.statmethods.net/
- An R language tutorial for people who already know SAS, SPSS, or Stata. By Robert Kabacoff.
- Some Hints for the R Beginner: http://www.burns-stat.com/
- Click on "Tutorials" to get started with R. By Patrick Burns.
Sources of Help
- The R search engine: http://rseek.org/
- Looking for something? Start here. This search engine focuses on R-related web sites.
- The R Help mailing list: https://stat.ethz.ch/mailman/listinfo/r-help
- An incredible amount of Q&A. Got a question? First, search the archives (http://tolstoy.newcastle.edu.au/R/), since it's likely that someone already answered your question. Second, post your question if necessary, but read the posting guide before you do (http://www.r-project.org/posting-guide.html).
- Stack overflow: http://stackoverflow.com/questions/tagged/r
- Ask a question, get an answer from an expert. Use the "r" tag.
- Twitter: http://twitter.com/#search?q=%23rstats
- If you can squeeze your question into 140 characters, someone might know the answer. Use the #rstats hashtag.
Books
- R in a Nutshell by Joseph Adler (http://www.amazon.com/dp/059680170X)
- A combination tutorial and reference book.
- A Beginner's Guide to R by Zuur, Ieno, and Meesters (http://www.amazon.com/dp/0387938362)
- A quick-start guide for newbies.
- Using R for Introductory Statistics by John Verzani (http://www.amazon.com/dp/1584884509)
- A good introduction to both R and statistics.
- R Graphics by Paul Murrell (http://www.amazon.com/dp/158488486X)
- If you want to understand graphics in R, this is the book.
- Modern Applied Statistics with S (4th ed.) by Venables and Ripley (http://www.amazon.com/dp/1441930086)
- Especially good for anyone who knows statistics and wants to start using R.
©
R: Getting started with R
Main page on R
Don't try to do too much in one session. You might like to just install R and then come back and do further steps such as going through the Venables and Ripley tutorial and installing libraries. Finally you will want to use project directories in which you keep data, scripts and other files pertaining to a particular project.
For a quick start, focus on the items that are marked QUICK START:.
You should consider installing RStudio, a recently released IDE (Integrated Development Environment) for using R that has gained rapid popularity.
You will eventually need to install additional packages which you can do easily whenever your computer is connected to the internet.
Recently released RStudio, which is free, provides an integrated environment including a script editor that will probably displace previously available editors.
RStudio has probably superseded previous R editors but for historical completeness one can metion a number of other possibilities, some of which are free. Emacs is very powerful but difficult to learn, Tinn-R (see below) under Windows is easier to install and use but suffers from less than perfect reliability. WinEdt is not expensive (Academic: US$40) and is considered a very good editor with an interface, R-WinEdt designed for R.
A more primitive but simple way to keep output and selected graphs so they can eventually become part of a report is to copy and paste output and graphs into a Word file. Graphs can be copied and pasted as 'Windows metafiles' without loss of resolution. Text in the R output window should be pasted with a 'fixed width font' such as Courier New.
At this stage, have a look at R basics on the UCLA ATS web site.
Generate links to a set of lessons people can use to learn R. Each lesson should take approximately 1 hour and contain exercises.
Use stuff from here: Getting Started page on the old math wiki
Don't try to do too much in one session. You might like to just install R and then come back and do further steps such as going through the Venables and Ripley tutorial and installing libraries. Finally you will want to use project directories in which you keep data, scripts and other files pertaining to a particular project.
For a quick start, focus on the items that are marked QUICK START:.
Contents[hide] |
Installing R
Windows, Mac or Linux
QUICK START: To install R, you can go to the CRAN (Comprehensive R Archive Network website) and follow the instructions.You should consider installing RStudio, a recently released IDE (Integrated Development Environment) for using R that has gained rapid popularity.
You will eventually need to install additional packages which you can do easily whenever your computer is connected to the internet.
Further information
You can get more information on installing R and RStudio at John Fox's website.Starting to learn R: Tutorials on the Web
Start by working through sample scripts. This is probably the best way to start exploring and enjoying R without getting overwhelmed by long explanations.- QUICK START: Work through the sample scripts prepared by John Fox for his UCLA tutorial. R will reveal itself to you as you try them out. Print a copy of Tom Short's R reference card and read William Revelle's Using R for psychological research: A Simple Guide to an Elegant Package.
- A list of tutorials recently (January 2012) recommended on the LinkedIn blog The R Project for Statistical Computing:
- R tutorial at cyclismo.org
- UCLA's Academic Technology Services resources for learning R
- Quick-R
- The Georgia R School
- Official Introduction to R at CRAN. Start with the sample session in Appendix A.
- Vincent Zoonekynd's Statistics with R site
- R by Example
- Statistics, R, Graphics and Fun
- A short course in R by Thomas Lumley
- icebreakeR by Andrew Robinson
- Hadley Wickam's wiki (more advanced)
- A blog for useful ideas
- Finally if you're an experienced programmer just getting starting with R you might really enjoy The R Inferno by Patrick Burns
- An annotated list elsewhere on this wiki: R: R tutorials and courses.
- An extensive list of on-line books and tutorials is available from the CRAN site.
- Chris Green at the University of Washington has an excellent very accessible on-line book: Christopher Green: R Primer
- An introductory sample session: http://cran.r-project.org/doc/manuals/R-intro.html#A-sample-session
- The very basics from a tutorial at the University of Waterloo.
- The first chapter of Venables and Ripley adapted to R VR4: Chapter 1 summary. This is more of an introductory session than a tutorial. It gives a good overview of the potential in using R as well as introducing a number of interesting statistical ideas.
- The tutorial that comes with R[1] is extensive but gradually working through it might be the best way to become proficient.
- R tutorial at UCLA This is good but takes you through some things you won't really need.
- The start of a local tutorial (please contribute)
- If you are already familiar with SAS or SPSS you should have a look at Rob Munchen (2007) R for SAS and SPSS Users
Other interesting tutorials on the web
- An easy-to-follow introduction that shows how to fit simple regression and create few basic graphs
- Wikiversity: How to use R
- Using R for psychological research: A very simple guide to a very elegant package
- Notes on the use of R for psychology experiments and questionnaires by Baron and Li
- John Verzani's simpleR
- Statistical Computing course at Illinois State University
- A list of R resources for beginners partly copied from Ulrich Halekoh, Søren Højsgaard (March 2006) R Installation - getting the R program up and running it on Windows Version 4:
- The list of R documentation on the CRAN website.
- Petra Kuhnert and William Venables have an excellent 360-page textbook “An Introduction to R: Software for Statistical Modelling & Computing” that can be downloaded as a zip file. It contains extensive information on using Tinn-R and "Lab Exercises" in Appendix II.
- R for beginners by Emmanuel Paradis: http://cran.r-project.org/doc/contrib/Paradis-rdebuts_en.pdf
- Short introduction to the basic features of R. A good point to start.
- Simple R 2 by John Verzani: http://cran.r-project.org/doc/contrib/Verzani-SimpleR.pdf
- Introduction to R with a lot of statistical examples
- An Introduction to R: Software for Statistical Modelling & Computing 3 by Petra Kuhnert and Bill Venables:
- Extensive non-technical coverage of R, 364 pages
- An introduction to R 4 by The R core team: http://cran.r-project.org/doc/manuals/R-intro.pdf
- Reference introductory text for using R.
- Commented literature list: http://gbi.agrsci.dk/biomet/public_html/statmaster/course_information/literature.pdf
- Getting Started in R by S.A.Bashir : http://www.luchsinger-mathematics.ch/Bashir.pdf#search=%22Getting%20Started%20R%22
Script Editors
The built-in editor lacks some desirable features. For example, under Windows, it does not show matching parentheses. You will probably want to get a separate editor.Recently released RStudio, which is free, provides an integrated environment including a script editor that will probably displace previously available editors.
RStudio has probably superseded previous R editors but for historical completeness one can metion a number of other possibilities, some of which are free. Emacs is very powerful but difficult to learn, Tinn-R (see below) under Windows is easier to install and use but suffers from less than perfect reliability. WinEdt is not expensive (Academic: US$40) and is considered a very good editor with an interface, R-WinEdt designed for R.
Keeping your work
Probably, the ideal way (as of the fall of 2013) to keep output and to perform analyses in a way that is reproducible is to use R Markdown in RStudio.A more primitive but simple way to keep output and selected graphs so they can eventually become part of a report is to copy and paste output and graphs into a Word file. Graphs can be copied and pasted as 'Windows metafiles' without loss of resolution. Text in the R output window should be pasted with a 'fixed width font' such as Courier New.
Using and installing packages
Many packages come with R. To use them in an R session, you need to load the package. For example to load the MASS package which contains functions and datasets that accompany Venables and Ripley, Modern Applied Statistics with S, you use the command:> library(MASS)To get an overview of what's available in MASS, you use:
> library(help=MASS)
Installing additional packages
Some packages are not automatically installed when you install R but they need to be downloaded and installed individually. An important example is the 'car' package that accompanies Fox, Applied Regression. You install it with the R command:> install.packages("car")After installing it you load it the same way as a pre-installed package, i.e.
> library(car)To get information about the package, use:
> library(help = car)On your own computer, the package needs to be installed only once. On a lab computer you may need to reinstall in each new session.
At this stage, have a look at R basics on the UCLA ATS web site.
Setting up project directories
RStudio provides facilities for managing project directories. If you are working only with R you can organize your work in project directories as follows:- Create a directory for your project.
- Copy a workspace (a .Rdata file) to the directory
- You can then start R by clicking on the .Rdata file's icon
- All directory references in the R session will be relative to the project directory. For example, you can read a file 'data.csv' in the directory with
> data <- read.csv('data.csv')
R lessons
Template:IncompleteGenerate links to a set of lessons people can use to learn R. Each lesson should take approximately 1 hour and contain exercises.
R Tip sheets
Exploring more deeply
- Wikibook: R Programming
- Try R FAQ
- An advanced introdution[2]
- A good tutorial with lots on graphics etc.[3]
- http://www.nettakeaway.com/tp/?s=R
- A wiki for R tipe
- R for SAS and SPSS Users
Exploring much more deeply
This is not up to date. Please help- R Portal at UCLA[4]
- On-line book: Using R for Data Analysis and Graphics: Introduction, Code and Commentary by J H Maindonald updated: 14 November 2004 with on-line resources.
Courses in specialized areas
- Graphics in R: http://csg.sph.umich.edu/docs/R/graphics-1.pdf
Later
IncompleteIntroductions to R
- An Intruction to R|[html version
- Statistics with R
- Rtips Very extensive collection of tips gleaned from the R mailing list
- Applied Statistics With R by John Fox at Wien, May/June 2006
Materials from John Fox
Materials from John Fox- Learning R
- An Introduction to Statistical Computing in R (February 2005, California Center for Population Research, UCLA)
- An R and S-PLUS Companion to Applied Regression John Fox (especially see Scripts for examples by chapter and appendix , Web appendix to the text)
- Software Add Ons
- Rcmdr package for R (a basic-statistics graphical-user-interface for R).
- car (Companion to Applied Regression) package for R and library for S-PLUS. Software associated with An R and S-PLUS Companion to Applied Regression.
- effects (R package for effect displays).
Use stuff from here: Getting Started page on the old math wiki
- UCLA page on Installing, Customizing, and Updating R
- A wiki page on getting started with R
- UCLA page on learning R
Getting Started with the R Data Analysis Package
Professor Norm Matloff
Dept. of Computer Science
University of California at Davis
Davis, CA 95616
R is a wonderful programming language for statistics and data management, used widely in industry, business, government, medicine and so on. And it's free, an open source product. The S language, of which R is essentially an open source version, won the ACM Software System Award in 1998.
You can download R from its home page.
For Ubuntu Linux or other Debian-related OSs, a more direct method is:
I'll list a few tutorials below (not necessarily the best, just ones I know of). But first, I wish to make a very important point:
Dept. of Computer Science
University of California at Davis
Davis, CA 95616
R is a wonderful programming language for statistics and data management, used widely in industry, business, government, medicine and so on. And it's free, an open source product. The S language, of which R is essentially an open source version, won the ACM Software System Award in 1998.
Downloading R:
R is available for Linux, Windows and Mac systems.You can download R from its home page.
For Ubuntu Linux or other Debian-related OSs, a more direct method is:
% sudo apt-get install r-base
Learning R:
There is a perception among some that R has a steep learning curve, but I disagree. True, R usage has its advanced aspects, but my recommendation is simply, just get started! Start simple, and then refine gradually.I'll list a few tutorials below (not necessarily the best, just ones I know of). But first, I wish to make a very important point:
"When in doubt, try it out!" That's a slogan I invented to illustrate the point that R's interactive mode allows you to try your own little experiments, the best way to learn. Keep this in mind when you go through the tutorials listed below and in Google.Here are some resources that I would recommend for learning R:
- For Non-Geeks:
There are numerous gentle online tutorials on R,
such as:
- This one at Potsdam University.
- A gentle R tutorial from the famous O'Reilly book series.
- Just plug "R tutorial" into Google to see many more.
- For (Actual or Aspiring) Geeks:
R syntax is similar to those of C, Python, PERL, etc.
don't know these terms) object-oriented and has a
functional programming philosophy. Here are some introductions to R
from a programming perspective:
- Of course, I'll mention my own tutorials on writing R code
(though very little on the statistical aspects):
- My 5-minute tutorial on R programming for those who know C.
- Chapter 1 of my book on R software development, The Art of R Programming, NSP, 2011. This gives you an overview of the language, and walks you through a couple 5-minute R sessions.
- Part of a very rough draft of that book. It is only about 50% complete, has various errors, and presents a number of topics differently from the final version, but should be useful.
- The R programming Wiki.
- Of course, I'll mention my own tutorials on writing R code
(though very little on the statistical aspects):
- Many books are listed on the R home page.
Advanced R:
- parallel R:
R Programming Tools:
One of the most debated topics in R online discussions is that of programming tools for R, of which there are many.- I'm not a fan of integrated development environments, but if you
like IDEs, there are a number of open source products available:
- The most popular is undoubtedly RStudio, RStudio, introduced in 2011, and growing rapidly in functionality.
- For fans of the Eclipse framework, StatET is available, and includes a debugging tool.
- More established products include JGR, Rcmdr, RKWard.
- There are a number of plugins for text editors such as Emacs (for which a debugger is available), Vim, gedit, and so on.
- In the commercial realm, there is one from Revolution Analytics , which also includes a debugging tool.
People you can talk to:
- There are various mailing lists (start with R-help) shown on the R home page.
- There are R user groups in cities around the world. I'm active in the the San Francisco Bay Area group. We hold meetings once a month, with one or two speakers. Many attendees are new to R.
- Another online place to ask questions is Stack Overflow.
R в C++
Последнее время, по теме R на хабре появляются все новые посты: habrahabr.ru/post/161625/#habracut, habrahabr.ru/post/162583/. И мне захотелось обратить внимание на один важный момент. R – это не только язык программирования, но и огромная математическая библиотека, с уклоном в статистическую обработку данных. В данной статье я хотел бы рассказать, как использовать R в программах, написанных на с++.
В своем составе R имеет сишный интерфейс, однако в нем представлено далеко не все. Чтобы использовать возможности R на полную, существуют специальные пакеты: Rcpp и RInside. Пример, рассматриваемый в данном посте писался под Ubuntu 12.10, хотя, насколько мне известно все необходимое есть и для Windows.
Установка необходимых пакетов
Устанавливаем R и Rcpp: apt-get install r-base r-cran-rcpp2. Rcpp – библиотека для интеграции R в C++.
2. Запускаем R (в командной строке набираем: R) и устанавливаем дополнительные пакеты, из командной строки:
RInside – это враппер над Rcpp, который делает работу с Rcpp очень простой
install.packages(«RInside»), если не получилось скачиваем архив с cran.r-project.org/web/packages/RInside/index.html, затем опять в командной строке R: install.packages(file.choose(), repos=NULL), выбираем архив.
Fitdistrplus – пакет расширяющий набор математических функции R
install.packages(«fitdistrplus»), если не получилось скачиваем архив с cran.r-project.org/web/packages/fitdistrplus/index.html, затем опять в командной строке R: install.packages(file.choose(), repos=NULL), выбираем архив.
Теперь задача
Например, необходимо определить гипотезу распределения случайной величины.
Сначала рассмотрим как это выглядит в R:
library(fitdistrplus) #Загрузим пакет, который легко позволяет определять основные моменты распределения.
x = rnorm(1000, 10, 5) #Запишем в x 1000 значений с нормальным распределением, математическое ожидание – 10, ср. кв. отклонение - 5
plot(x) #Посмотрим график
Для определения гипотезы распределения необходимо знать параметры распределения случайной величины. Найдем параметры (для нормального распределения: мат ожидание и ср. кв. отклонение) и запишем в переменные mean и sd.
params = fitdist(x, "norm");
mean = params[[1]][[1]]
sd = params[[1]][[2]]
Теперь проверим гипотезу распределения на нормальный закон. Для этого будем использовать тест Колмогорова – Смирнова. При уровне значимости, альфа = 0.05 гипотеза должна быть отвергнута если альфа > p-value.
ks.test(x, "pnorm", mean, sd)
#out:
#One-sample Kolmogorov-Smirnov test
#data: x
#D = 0.0199, p-value = 0.8236
p-value = 0.8236 > alfa -> гипотеза устраивает
Теперь проведем тот же тест, но в качестве исходных данных подсунем случайную величину распределенную по равномерному закону.
y = runif(1000, 0, 20)
plot(y)
params = fitdist(y, "norm");
mean = params[[1]][[1]]
sd = params[[1]][[2]]
ks.test(y, "pnorm", mean, sd)
#out
#One-sample Kolmogorov-Smirnov test
#data: y
#D = 0.0659, p-value = 0.0003399
как видно p-value получился меньше alfa, следовательно гипотезу можно отвергнуть
Теперь попробуем повторить примерно тоже из c++.
Перед сборкой необходимо настроить пути на три пакета:
R, Rcpp, RInside (туда куда поставили):
Например R/include; Rcpp/include; RInside/include
R/lib; Rcpp/lib; RInside/lib
В этом примере мы создадим массив случайных чисел от 0 до 1000 и попробуем проверить гипотезу, что случайная величина распределена по нормальному закону.
Основная суть работы с R в C++ заключается в том, что мы заполняем строки на языке R и при помощи классов RInside и Rcpp выполняем их.
Результат выполнения программы:
p.value is 0.000394703
Т.е. мы выбрали не верную гипотезу.
Заключение
Наверное код в данной статье не идеален, но цель была рассказать о возможности довольно просто использовать весь функционал R на c++, кроме того существуют библиотеки для подключения R к другим языкам, например java.
Ссылки
Вся информация доступна на вики en.wikipedia.org/wiki/R_(programming_language) + две статьи с хабра.
©
В своем составе R имеет сишный интерфейс, однако в нем представлено далеко не все. Чтобы использовать возможности R на полную, существуют специальные пакеты: Rcpp и RInside. Пример, рассматриваемый в данном посте писался под Ubuntu 12.10, хотя, насколько мне известно все необходимое есть и для Windows.
Установка необходимых пакетов
Устанавливаем R и Rcpp: apt-get install r-base r-cran-rcpp2. Rcpp – библиотека для интеграции R в C++.
2. Запускаем R (в командной строке набираем: R) и устанавливаем дополнительные пакеты, из командной строки:
RInside – это враппер над Rcpp, который делает работу с Rcpp очень простой
install.packages(«RInside»), если не получилось скачиваем архив с cran.r-project.org/web/packages/RInside/index.html, затем опять в командной строке R: install.packages(file.choose(), repos=NULL), выбираем архив.
Fitdistrplus – пакет расширяющий набор математических функции R
install.packages(«fitdistrplus»), если не получилось скачиваем архив с cran.r-project.org/web/packages/fitdistrplus/index.html, затем опять в командной строке R: install.packages(file.choose(), repos=NULL), выбираем архив.
Теперь задача
Например, необходимо определить гипотезу распределения случайной величины.
Сначала рассмотрим как это выглядит в R:
library(fitdistrplus) #Загрузим пакет, который легко позволяет определять основные моменты распределения.
x = rnorm(1000, 10, 5) #Запишем в x 1000 значений с нормальным распределением, математическое ожидание – 10, ср. кв. отклонение - 5
plot(x) #Посмотрим график
Для определения гипотезы распределения необходимо знать параметры распределения случайной величины. Найдем параметры (для нормального распределения: мат ожидание и ср. кв. отклонение) и запишем в переменные mean и sd.
params = fitdist(x, "norm");
mean = params[[1]][[1]]
sd = params[[1]][[2]]
Теперь проверим гипотезу распределения на нормальный закон. Для этого будем использовать тест Колмогорова – Смирнова. При уровне значимости, альфа = 0.05 гипотеза должна быть отвергнута если альфа > p-value.
ks.test(x, "pnorm", mean, sd)
#out:
#One-sample Kolmogorov-Smirnov test
#data: x
#D = 0.0199, p-value = 0.8236
p-value = 0.8236 > alfa -> гипотеза устраивает
Теперь проведем тот же тест, но в качестве исходных данных подсунем случайную величину распределенную по равномерному закону.
y = runif(1000, 0, 20)
plot(y)
params = fitdist(y, "norm");
mean = params[[1]][[1]]
sd = params[[1]][[2]]
ks.test(y, "pnorm", mean, sd)
#out
#One-sample Kolmogorov-Smirnov test
#data: y
#D = 0.0659, p-value = 0.0003399
как видно p-value получился меньше alfa, следовательно гипотезу можно отвергнуть
Теперь попробуем повторить примерно тоже из c++.
Перед сборкой необходимо настроить пути на три пакета:
R, Rcpp, RInside (туда куда поставили):
Например R/include; Rcpp/include; RInside/include
R/lib; Rcpp/lib; RInside/lib
В этом примере мы создадим массив случайных чисел от 0 до 1000 и попробуем проверить гипотезу, что случайная величина распределена по нормальному закону.
Основная суть работы с R в C++ заключается в том, что мы заполняем строки на языке R и при помощи классов RInside и Rcpp выполняем их.
#include <RInside.h> int main(int argc, char *argv[]) { std::string evalstr = ""; // строка для формирования кода на R RInside R(argc, argv); //окружение Rcpp::NumericVector RndVec(1000); // создаем массив чисел for(int i = 0; i < 1000; ++i) RndVec(i) = (float)(rand() % 100); // заполняем его R["RndVec"] = RndVec; // связываем с массивом в R SEXP ans; // результат // формируем строку для R: // пробуем получить параметры распределения, считая, что это нормальный закон evalstr = "library(fitdistrplus) \n \ out <- fitdist(RndVec, \"norm\", \'mme\')[[1]][[1]]; print(out); out"; // получили результат ans = R.parseEval(evalstr); // получили матожидание Rcpp::NumericVector mean(ans); std::cout << "mean " << " is " << mean[0] << std::endl; evalstr = "out <- fitdist(RndVec, \"norm\", \'mme\')[[1]][[2]]; print(out); out"; ans = R.parseEval(evalstr); // получили ско Rcpp::NumericVector sd(ans); std::cout << "sd " << " is " << sd[0] << std::endl; R["curMean"] = mean[0]; R["curSd"] = sd[0]; // выполнили тест evalstr = "out <- ks.test(RndVec, \"pnorm\", curMean, curSd)[[2]]; print(out); out"; ans = R.parseEval(evalstr); Rcpp::NumericVector v1(ans); // посчитали p.value std::cout << "p.value " << " is " << v1[0] << std::endl; return 0; }
p.value is 0.000394703
Т.е. мы выбрали не верную гипотезу.
Заключение
Наверное код в данной статье не идеален, но цель была рассказать о возможности довольно просто использовать весь функционал R на c++, кроме того существуют библиотеки для подключения R к другим языкам, например java.
Ссылки
Вся информация доступна на вики en.wikipedia.org/wiki/R_(programming_language) + две статьи с хабра.
©
Installing R in Ubuntu
Universe
The current version of R available when a new version of Ubuntu is released is always available in the universe repository. To install R:
sudo apt-get install r-base
R package installation in ubuntu
install.packages("fitdistrplus")
©
The current version of R available when a new version of Ubuntu is released is always available in the universe repository. To install R:
sudo apt-get install r-base
R package installation in ubuntu
install.packages("fitdistrplus")
©
Subscribe to:
Posts (Atom)