Unformatted text preview:

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 On a Macintosh the console window will be floating on the desktop instead of in the RGui window Also as of this writing the current version is 2 4 rather than 2 0 1 Note 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 PSY 394U Do It Yourself Statistics 1 1 always hit return or enter at the end of the line R should respond with the hopefully correct answer 1 2 Don t worry about the 1 it s just telling us that 2 is the first item in our answer Since our answer only has one item it s not very useful in this case 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 Excel SPSS etc Store something into a variable called x x 1 1 Note that we do not get an output it has been stored rather than printed on the console To see the value of a variable just type the variable name x 1 2 Variable names can be more than just a single letter so they can be a little more descriptive nqp 22 7 You can also make them really descriptive but they get cumbersome to type not quite pi 22 7 Notice I was able to use periods to make the variable name more readable You can use numbers too but avoid using anything else Also variable names must begin with a letter and they are case sensitive as is everything else in R So X is not the same variable as x We can test the equality or inequality of variables The variable pi is built into R so nqp pi Note the double equals which means are these equal A single equals sign would have overwritten the old value of nqp with the value of pi Let s see how bad the approximation is pi nqp PSY 394U Do It Yourself Statistics Okay then perhaps you can guess what this returns nqp pi Note that command prompt is special and not part of the command per se but the you type is interpreted as greater than You can see what variables you have created by listing them with the list command ls And you can remove a variable with the remove command like this rm nqp Whoops maybe we didn t want to do that Unfortunately there is no way to undo deleting something npq is now gone forever We can however easily re do the earlier command that created it To re do a command you can hit the up arrow on your keyboard to go back through previously entered commands When you see the one you want just hit return If you overshoot it hit the down arrow Let s clean up by removing all the variables rm list ls Here we passed a listing of all variables to the remove command List is a special argument associated with the remove rm command that enables you to remove everything that is returned with the ls command that we entered above Don t worry if this 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 rm or 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 rm list ls You will see that the command prompt has changed to a This means that R has recognized that the command is incomplete and is waiting for you to finish it Simply type the final and R will continue as normal Sometimes you may get the and have no idea how to complete the command In this case just hit the escape key to get back to the normal command prompt Beyond single numbers PSY 394U Do It Yourself Statistics Variables can hold more than one number note a variable that holds a single value is often called a scalar and one that holds multiple values is often called a vector x 1 5 This makes x to 1 2 3 4 5 Don t take my word for it verify x You can also enter numbers into a variable by hand y c 2 3 5 8 10 which means concatenate the numbers 2 3 5 8 10 and assign them to y When working with these kinds of variables R behaves in a convenient fashion x 5 No need to add 5 to each and every element of x R automatically interprets the command this way If we add two vectors x y Again the result is sensible R adds each value of y to the corresponding value of x Comparisons work the same x 3 x y 99 999 of the time approximately you will perform operations or comparisons using either two vectors of the same length incl two scalars or a vector and a scalar but you don t have to z c 1 2 x z Interesting we got an output and a warning but it s not obvious what is going on Try x z See what happened to produce the output At least R was nice enough to generate a warning Like I said you will rarely if ever have to do this PSY 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 cor x y Some connecting lines would make the form of the relationship a little clearer lines x y This looks sigmoidal s shaped to me but I ll bet a straight line still does a pretty good job of describing the data So let s fit a linear model y as a linear function of x with the command lm and assign the output to a variable m lm y x And view a summary of the output summary m Whoa a lot of stuff here But notice we got the important things for a straight line fit namely the slope and y intercept of the line and the amount of variance explained by the line fit the adjusted R square Perhaps more interestingly we can just look at the result abline coef m col red Again don t worry …


View Full Document

UT PSY 394U - Chapter 1 A sample R session

Documents in this Course
Roadmap

Roadmap

6 pages

Load more
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 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?