![]() (Do not save it in a protected area such as Programs in the Windows operating system.) Be sure to “unzip” the file or “extract all” of the programs from the zipped folder. Save the zip file at a convenient location on your computer where you normally save your data files and documents. Be sure the site is legitimate before downloading anything to your computer.) Specifically, find the link to the zip file that contains all the programs used with this book. If either of the addresses stated here does not work, please search the web for “Doing Bayesian Data Analysis” in quotes. For the latest updates about programs used with this book, see the book's web site at, and the book's blog at. The program above is stored as a file named SimpleGraph.R in the folder of programs that accompany the book. 3.2.1 Get the programs used with this book Anything on a line after the comment symbol is ignored by R. A comment in R begins with the “ #”symbol, called a number sign or a pound sign. The command lines above include comments on each line to explain to the human reader what the line is intended to do. Plot( x, y, col=“skyblue”, type=“l” ) # Plot the x,y points as a blue line. Y = xˆ2 # Specify corresponding y values. X = seq( from = -2, to = 2, by = 0.1 ) # Specify vector of x values. Inside R, the variable y now refers to a vector of 41 values: 4.0, 3.61, 3.24, …, 4.0. R interprets “ ˆ” to mean raising values to a power. Next we tell R to create the corresponding y values. We’ll discuss vectors and other structures in more detail later. This sort of ordered list of numerical values is called a “vector” in R. Inside R, the variable x now refers to a list of 41 values: −2.0, −1.9, −1.8, …, +2.0. We have R set up the list of x values by using the built-in sequence function: x = seq( from = -2, to = 2, by = 0.1 ). Let's arbitrarily select x values from −2 to +2, separated by intervals of 0.1. We must tell R where all those densely packed points should be positioned.Įvery point is specified by its x and y coordinates, so we have to provide R with a list of x values and a list of corresponding y values. The example in the next section should help clarify.Īs a simple example of what R can do, let's plot a quadratic function: y = x 2.What looks like a smooth curve on a graph is actually a set of points connected by straight lines, but the lines are so small that the graph looks like a smooth curve. ![]() To run a program stored in a file named MyProgram.R, we could type “ source(“MyProgram.R”)” at R's command line, or click the “source” button in the R or RStudio menu. In R, running a script is called source-ing the script, because you are redirecting the source of commands from the command line to the script. Then we could run the program and R would step through the list of commands, in order. ![]() We could save the list of commands as a program, or script, in a simple text file. This is because R assumes that when you type in x+x, you are really asking for the value of the sum of the value of x with the value of x, and you are not asking for an algebraic reformulation such as 2*x. For example, you could first type in x=2 and then, as a second command, type in x+x, to which R will reply 4. ![]() Throughout this book, programming commands are typeset in a distinctive font, like this, to distinguish them from English prose and to help demarcate the scope of the programming command when it is embedded in an English sentence.Ī program, also known as a script, is just a list of commands that R executes. As we will see later, variables in R are often multi-component structures, and therefore it can be informative to the user to display which component is being shown. The answer, of course, is 5, but the line begins with a bracketed “ ” to indicate that the first component of the answer is 5. Again, the “ >“ symbol above indicates the R command prompt, at which we typed “ 2+3.” The next line above, “ 5,” shows R's reply.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |