Unformatted text preview:

MAT167 Home Work: Intro to R Tanbakuchi(Compile date: Sat Jan 31 10:52:08 2009).The following homework will help you get acquainted with the R statistical software package.It will seem unfamiliar and awkward at first, but stick with it, soon it will be easy. Don’t getfrustrated if this HW seems difficult; this is as hard as it gets. I will never expect you to memorizeR commands for exams, the quick reference sheet on the website will be provided. During thesecond half of the course you will find R to be an extremely helpful resource.If you get stuck with the HW, then take a look at take a look at the R help page: http://tanbakuchi.com/Resources/R_Statistics/RBasics.html. If that doesn’t help, then send anemail to me explaining the problem you are having. Be sure to copy and paste the your R work(and output with errors) into the email.Some helpful notes:Implicit multiplication signs Make sure you include all implicit multiplication signs. If youget either of the following errors: syntax error or attempt to apply non-function, youprobably forgot to include the multiplication sign *. You will get an error if you type 2a or3(4-2), you should type 2*a or 3*(4-2).Order of operations be sure to enter parenthesis when needed. R observes the normal order ofoperations. Thus2+63should be entered as (2+6)/3.Powers in R use the carrot symbol, ie. 24is entered as 2∧4.Square Root To find the square root in R, use the sqrt(x) function, ie.√16 is entered assqrt(16).Closing parenthesis Make sure you include closing parenthesis and quotations. Typing sqrt((2+4)*3won’t work since the closing parenthesis for the square root function is missing. The correctexpression is sqrt((2+4)*3) which has the closing parenthesis. If the R prompt changesfrom > to + it indicates you are missing a closing parenthesis or quotation. Type the closingelement and hit enter. If you can’t get the > prompt back, quit and reopen R.Copy your work into a word document (including any plots). Ensure it is labeled withthe question numbers and neat. Only include the correct work, do not include errors.1. Use R as a calculator to verify that the following statements are true (by evaluating the lefthand side to check that it is equal to the right hand side).(a) 12 × 2 − 4.8 = 19.2(b)83+ 24= 128.5 (1)(c) cos(0) = 1(d)√8 = 2.82842712474619(e)q8+435= 3.193743884534262. Define the following variables in R: a = 5, b = 12.3. Use R to show that the the followingstatements are true. (If you want to check to see what value is stored in a variable, just typeits name and hit enter.) Don’t forget to include implicit multiplication signs.MAT167 Home Work: Intro to R Tanbakuchi(a) 3.5a = 17.5(b) a − b = −7.3(c)12−5b− 5.2a= −3801.5(d) (b − a)(2a − b) = −16.793. Define the vector (data set) w = {−5, 4, 2, 0, 3, 1, −2, 4} in R. Answer the following questions.Type the following commands in R, look at the output and then write one or two completesentences describing what the command did. (Be sure to include your input and output.)To create the vector1w you type: w=c(-5, 4, 2, 0, 3, 1, -2, 4)(a) w*2(b) w[1](c) w[2](d) w==4(e) w>2(f) w[w>2](g) What would you type in R to find all the values in w that are less than 0?4. Define the following vectors in R just as you did for w in the previous question:y = {65, 22, 14, 19, 20}z = {8, 3, 2, 5, 7, 8}(a) To sum up all the numbers in a vector x, you can use the function sum(x). Thus, to findthe sum of all the values in y you would type:> sum(y)[1] 140Use R to find the sum of all the values in z.(b) The function max(x) returns the maximum value in a vector. Thus, to find the maximumvalue in z you would type:> max(z)[1] 8Use R to find the maximum value in y.5. R is capable of making many types of graphs. We can use R’s curve function to plot polyno-mials.(a) Type in the following command: curve(sin(x*2*pi))What function did R plot?(b) What is the range of x values plotted for the previous graph you made?1Throughout this course we will use this method to store a set of data in a variable. Make sure you know how todo this!Page 2MAT167 Home Work: Intro to R Tanbakuchi(c) Now type in: curve(sin(x*2*pi), xlim=c(-2, 2))We now have added an optional argument to the function which changes the defaultbehavior. What is the new range of x values plotted on the graph?(d) What is the default range of x values plotted for the curve function?(e) What does the optional argument xlim do?(f) What would you type into R to make the the above graph have a x range of (0, 5)?(g) Type the following command: curve(x∧3, xlim=c(-10, 10), main="Polynomial")This time we are graphing f (x) = x3. What does the optional argument main do?6. Use the curve() function in R to plot the following function over the domain (−10, 20). Setthe title of the plot to “Parabola”. (Be sure to copy and paste your plot into the HW.)f(x) = (x − 4)2+ 20 (2)7. Load the book data into R (download the .RData file on the website under the R resources anddouble click on it). This will load a bunch of data tables.(a) One of the data tables is named MM. This table contains information on the weights andcolors of M&M’s observed in a study. Type MM and hit enter. This will display the datain the table. What are the column names (you may have to scroll up)?(b) An easier way to determine the names of the columns is to use the names() function. Nowtype: names(MM). What did this do?(c) Type MM$WEIGHT. What did this do?(d) Now find the mean weight of the M&M’s using the above statement and the same methodwe used previously to find the mean of a vector.(e) Now make a histogram plot of the M&M weights by typing: hist(MM$WEIGHT)Hopefully now you can see how R is able to do allot of work with just a little typing.Yes, the trivial calculations can seem tedious, but more complex calculations and plots aremade easily!> hist(MM$WEIGHT)(f) Type the following: plot(MM$COLOR)Which color of M&M were observed the most in the study?(g) Type the following: summary(MM)What does the above command do?(h) Now find the mean weight of the blue M&M’s by typingblue=MM$WEIGHT[MM$COLOR=="Blue"]mean(blue)(i) Next find the mean weight of the green M&M’s by modifying what you did in the previousproblem.Page


View Full Document

UA MATH 167 - Study Notes

Download Study Notes
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 Study Notes 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 Study Notes 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?