DOC PREVIEW
UT PSY 394U - Chapter 1 A sample R session

This preview shows page 1-2-3 out of 8 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Some very basicsBeyond single numbersLarger data setsRepeating commands automatically (“looping”)PSY 394U – Do-It-Yourself Statistics Chapter 1 A sample R session If you have not already done so, download and install R on your computer. To do this, go to http://www.r-project.org/ and chose the download link from the menu on the left side of the page and follow the instructions. For now, you will only need the “base” version, not any of the contributed packages. Some very basics Start R on your computer. You should see a window something like this: n a Macintosh, the console window will be floating on the desktop instead of in the ote that the red block cursor is next to a “>”. This is called a “command prompt”. In this document, I will precede all commands you should actually enter with this prompt. For example, let’s start out by using R as a calculator: (ORGui window. Also, as of this writing, the current version is 2.4 rather than 2.0.1) NPSY 394U – Do-It-Yourself Statistics > 1+1 (always hit return or enter at the end of the line) should respond with the (hopefully) correct answer: 1] 2 on’t worry about the “[1]” – it’s just telling us that “2” is the first item in our answer. answer only has one item, it’s not very useful in this case. cel, SPSS, etc. x = 1+1 ote that we do not get an output; it has been stored rather than printed on the console. lue of a variable, just type the variable name: ble names can be more than just a single letter so they can be a little more e. ou can also make them really descriptive, but they get cumbersome to type: not.quite.pi = 22/7 otice I was able to use periods to make the variable name more readable. You can use g anything else. Also, variable names must begin with a tter, and they are case sensitive (as is everything else in R). So X is not the same , so: nqp == pi ote the double-equals, which means “are these equal?” A single equals sign would n the old value of “nqp” with the value of “pi”. et’s see how bad the approximation is: R [ DSince our An important aspect of programming languages is the ability to store values in variables,which are containers for data, similar to cells, rows, or columns in Ex Store something into a variable called x: > NTo see the va > x [1] 2 Variadescriptiv > nqp = 22/7 Y > Nnumbers too, but avoid usinlevariable as x. We can test the equality (or inequality) of variables. The variable “pi” is built into R > Nhave overwritteL > pi – nqpPSY 394U – Do-It-Yourself Statistics Okay, then perhaps you can guess what this returns: nqp > pi ote that command prompt is special and not part of the command per se, but the “>” erpreted as “greater than”) ls() nd you can remove a variable with the remove command like this: rm(nqp) hoops, maybe we didn’t want to do that. Unfortunately, there is no way to undo ething; npq is now gone forever. We can, however, easily re-do the earlier ommand that created it. To re-do a command, you can hit the “up arrow” on your you rm(list = ls()) ere, we passed a listing of all variables to the “remove” command. “List” is a special ith the remove (rm) command that enables you to remove verything that is returned with the ls() command that we entered above. Don’t worry if rm() or rm(list = ls() ou will see that the command prompt has changed to a “+”. This means that R has mmand is incomplete, and is waiting for you to finish it. Simply pe the final “)” and R will continue as normal. Sometimes, you may get the “+” and get > (Nyou type is int You can see what variables you have created by listing them with the list command: > A > Wdeleting somckeyboard to go back through previously entered commands. When you see the one want, just hit return. (If you overshoot it, hit the down arrow.) Let’s clean up by removing all the variables: > Hargument associated wethis command, or others below, seem mysterious to you; we’re just taking R for a test drive. Also, I know this command seems a bit cumbersome to type (why not just clear()?), but there’s nothing we can do about it. R, like anything else, has its downsides. Try typing the above command without the final closing parenthesis: > Yrecognized that the cotyhave no idea how to complete the command. In this case, just hit the “escape” key to back to the normal command prompt. Beyond single numbersPSY 394U – Do-It-Yourself Statistics Variables can hold more than one number (note: a variable that holds a single value is ften called a scalar, and one that holds multiple values is often called a vector): = to 1, 2, 3, 4, 5. Don’t take my word for it, verify: an also enter numbers into a variable by hand: numbers 2, 3, 5, 8, 10 and assign them to y”. on: add 5 to each and every element of x; R automatically interprets the command result is sensible; R adds each value of y to the corresponding value of x. the time (approximately), you will perform operations or comparisons using ectors of the same length (incl. two scalars), or a vector and a scalar, but you we got an output (and a warning), but it’s not obvious what is going on. Try: appened to produce the output? At least R was nice enough to generate a ike I said, you will rarely, if ever, have to do this. o > x = 1:5 This makes x > x You c > y = c(2, 3, 5, 8, 10) which means “concatenate the When working with these kinds of variables, R behaves in a convenient fashi > x + 5 No need tohis way. t If we add two vectors: > x + y Again, the Comparisons work the same: > x > 3 > x == y 99.999% ofither two vedon’t have to: > z = c(1,2) > x == z Interesting, > x + z See what harning. LwPSY 394U – Do-It-Yourself Statistics Now we can do some fun stuff: > plot(x, y) Looks like x and y are pretty highly correlated. Let’s see by how much: g lines would make the form of the relationship a little clearer: dal (“s-shaped”) to me, but I’ll bet a straight line still does a pretty good b of describing the data. So let’s fit a linear model - y as a linear function of x – with y of the output: tuff here. But notice we got the important things for a straight line fit, amely the slope and y-intercept of the line, and the amount of variance explained by the ystery commands.” Here, the command abline() lots a line given a slope and a y intercept (coefficients), which is what the command e real data, I’d


View Full Document

UT PSY 394U - Chapter 1 A sample R session

Documents in this Course
Roadmap

Roadmap

6 pages

Load more
Download Chapter 1 A sample R session
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Chapter 1 A sample R session and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Chapter 1 A sample R session 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?