Anda di halaman 1dari 5

Some R Commander Examples

Last update: Sunday, January 03, 2010

0.1 To open the R Commander window


Double click on R icon to run R. Once R is running, typing the command “library(Rcmdr)” into
the R Console will start the R Commander window.

(the version of R software installed in my computer is 2.9.2. The current version is 2.10.1)

0.2 To exist R and R Commander


To exit R Commander, use “File Æ ExitÆ From Commander ” from the R Commander menus
(located at the top of the R Commander window). To exit both R and R Commander, select “File
Æ ExitÆ From Commander and R ” from the R Commander menus.

1. To read a data set in text format:


The example text file named “FisherIris.txt” is stored at the link below:
http://www.ics.uci.edu/~zhaoxia/teaching/stat8/Data/FisherIris.txt
The first line of the file gives the names of the five variables, Sepal.Length, Sepal.Width,
Petal.Length, Petal.Width, and Species. Among them, the first four are continuous variables and
the last one is a categorical variable. The rest of lines contain the data values.

To read Fisher’s iris data into R Commander, select “Data Æ Import Data Æ from text file”
from the R Commander window menus. A pop-out dialog will tell you the default options for
reading a text file.

1
The default name is “Dataset”. You can change it to a name that makes sense to you. For
convenience, I name it ‘iris”. Since the first line of the text file contains the variable names, we
check “Variable names in file”. The third line asks you the symbols for missing value. The
default is “NA”. We don’t make any change because there is no missing value in the
FisherIris.txt.

If you choose to let R Commander read data from the URL of our course web, choose “Internet
URL” under “Location of Data File”. Alternatively, you can first download the data file to your
local disk and use the default the option, which is “Local file system”.

The file “FisherIris.txt” is a space-separated file, so no change was made on “Field Separator”.

The decimal-point character in “FisherIris.txt” is period. Therefore, we leave “Decimal-point


Character” unchanged.

2
Clicking the “OK” button at the bottom of the dialog brings up the “Internet URL” dialog. Then
copy the link address and click the “OK” button.

Now the data are read into R and the name to refer it is “iris”.

Once the data set is imported, you may modify or view the data by clicking the “Edit data set”
button (below the row with the menus) or the “View data set” button.

2. Histogram
Selecting “Graphs Æ Histogram” from the R Commander menus brings up a dialog box asking
for variable and other options.

3
Choosing the “Petal.Length” variable and and click the “OK” button results in an R Graphic
Device Window with the histogram of the petal length:

3. To save a graph
Select “Graphs Æ Save graph to file Æ…” from the R Commander menus and save your graph.
You will also need to specify the format and provide the location for the saved graph file.

4. Boxplot
Select “Graphs Æ Boxplot”. If your data are grouped, you may want to consider the “Plot by
groups” option. For example, to compare the petal lengths of the three iris species, choose “Plot
by groups” with the group variable “Species”.

4
5. Means, medians, and quartiles
Selecting “Statistics Æ Summaries Æ Numerical Summaries”, picking “Petal.Length” the
variable, and leaving the options unchanged gives the following result in the “Output Window”:
> numSummary(iris[,"Petal.Length"], statistics=c("mean", "sd", "quantiles"),
+ quantiles=c(0,.25,.5,.75,1))
mean sd 0% 25% 50% 75% 100% n
3.758 1.765298 1 1.6 4.35 5.1 6.9 150
For the Fisher’s iris data, we’d like to look at means of the three different iris species. To get
group means, simply click the “Summarize by groups” option and use “Species” as the group
variable. The result is:
> numSummary(iris[,"Petal.Length"], groups=iris$Species, statistics=c("mean",
+ "sd", "quantiles"), quantiles=c(0,.25,.5,.75,1))
mean sd 0% 25% 50% 75% 100% n
setosa 1.462 0.1736640 1.0 1.4 1.50 1.575 1.9 50
versicolor 4.260 0.4699110 3.0 4.0 4.35 4.600 5.1 50
virginica 5.552 0.5518947 4.5 5.1 5.55 5.875 6.9 50

6. Scatterplot, correlation, and regression line


Scatterplot: Graph → Scatterplot
Correlation: Statistics → Summaries → Correlation matrix
Regression: Graph → Scatterplot. Choose X- and Y-variables and check “Least-squares line”
under Options.

7. Linear regression model


Statistics Æ Fit models Æ Linear regression

8. To find proportions for a distribution


E.g., normal distributions:
Distributions → Continuous distributions → Normal distribution → Normal probabilities

Anda mungkin juga menyukai