Lab 7 – R Review and Debugging

advertisement
Lab 7 – R Review and Debugging
Date: October 4, 2011
Assignment Due Date: October 18, 2011
Goal: In this lab you will review the commands that we have used and learned up to now. You will be
creating a command cheat sheet that will help you in further coding. You will use previous labs or the
help menu to describe what each command does and write code to use it. You will also explore the
concept of debugging code by identifying errors in faulty code. Finally, you will plot polynomials
Review of R commands
Below is a list of the R commands that we have used in the lab up to this point. Today during the lab,
you will be filling out the table below to create a cheat sheet of commands. You can use the previous
labs or the help command(?) in order to help you. First describe what the command does. Be specific
and do not just type what the help menu gives you. Demonstrate that you understand. Next, describe
what arguments this function needs to run. Finally, write an example line of code to illustrate how to
use this command and what output your example code gives. Be sure to test each line of code in R!!! I
have done the first two commands for you as an example. SAVE THIS TABLE TO TURN IN and to use for
reference.
Command(s)
What does it do? Arguments?
Example Code
Output of
code
combines
Elements to
> X = c(1,3,5,7,9)
>X
arguments into a combine into a
[1] 1 3 5 7 9
c()
vector
vector
dev.copy()
dev.off()
exp()
for()
Saves current
figure to a jpeg
in the current
directory.
(jpeg,”name.jpg”)
No arguments for
dev.off()
>dev.copy(jpeg,”test.jpg”) Current figure
>dev.off()
saved as
“test.jpg” in
current
directory
function()
getwd()
lines()
log()
par()
plot()
points()
print()
rep()
seq()
title()
Debugging Code
Imagine you are trying to plot the cubic function, f(x)=x^3+7 from -10 to 10. Below I have written code
to attempt to do this but each code below is incorrect in some way. Identify the errors and explain how
to fix them. Before each section of code I will specify how many errors there are in the code.
2 ERRORS
# Code to plot the specified function f(x) = x^3 + 7 from -10 to 10
f = x^3 + 7
plot(x,f(x))
2 ERRORS
# Code to plot the specified function f(x) = x^3 + 7 from -10 to 10
f =function(x){ x^3 + 7}
plot(x,f)
1 ERROR
# Code to plot the specified function f(x) = x^3 + 7 from -10 to 10
x = -10:10
f = x^3 + 7
plot((f,x)
1 ERROR
# Code to plot the specified function f(x) = x^3 + 7 from -10 to 10
f = function(w){w^3 + 7}
x=seq(from=-10,to=10,by=.1)
plot(x,F(x))
1 ERROR
# Code to plot the specified function f(x) = x^3 + 7 from -10 to 10
x = -10:10
f =function(x){ x^3 + 7}
plot(x,f)
Now that you have identified the errors above write two different versions of code to make the desired
plot, one defining f as a function in R and one that defines f as a vector in R.
Polynomial Series
Consider the polynomials given below
where
Plot the first 6 polynomials in this series on the same figure. Use an input vector that goes from -3 to 3
with step size of .1. Label this plot and save it to include in your report. HINT: you must use par() to get
multiple plots of the same figure.
Download