Best MCQs Graph Plotting in R 17

The post is about MCQs Graph Plotting in R Language. There are 20 multiple-choice questions. The quiz covers the topics related to graphics devices in R Language, base and lattice graphics systems in R, the ggplot2 function, and parameters of different plot functions. Let us start with MCQs Graph Plotting in R Language.

The quiz is about Graphing and Plotting in R Language

1. Transparency is determined by which parameter of the rgb function?

 
 
 
 

2. Which of the following is an example of a valid graphics device in R?

 
 
 
 

3. What is a geom in the ggplot2 system?

 
 
 
 

4. What does the ‘pch’ option to par() control?

 
 
 
 

5. Under the lattice graphics system, what do the primary plotting functions like xyplot() and bwplot() return?

 
 
 
 

6. Which types of the plot does qplot plot?

 
 
 
 

7. Which of the following functions is typically used to add elements to a plot in the base graphics system?

 
 
 
 

8. The following code does NOT result in a plot appearing on the screen device.

library(lattice)
library(datasets)
data(airquality)
p <- xyplot(Ozone ~ Wind | factor(Month), data = airquality)

Which of the following is an explanation for why no plot appears?

 
 
 
 

9. The following code creates a scatterplot of ‘votes’ and ‘rating’ from the movies dataset in the ggplot2 package. After loading the ggplot2 package with the library() function, I can run

qplot(votes, rating, data = movies)

How can I modify the code above to add a smoother to the scatterplot?

 
 
 
 

10. Which function opens the screen graphics device on Windows?

 
 
 
 

11. Which of the following is an R package that provides color palettes for sequential, categorical, and diverging data?

 
 
 
 

12. What does the gg in ggplot2 stand for?

 
 
 
 

13. In the lattice system, which of the following functions can be used to finely control the appearance of all lattice plots?

 
 
 
 

14. Which of the following is the example of a vector graphics device in R?

 
 
 
 

15. What is ggplot2 an implementation of?

 
 
 
 

16. Which of the following is a basic workhorse function of ggplot2?

 
 
 
 

17. When I run the following code I get an error:

I was expecting a scatterplot of ‘votes’ and ‘rating’ to appear. What’s the problem?

 
 
 
 

18. Which function opens the screen graphics device for the Mac?

 
 
 
 

19. Bitmapped file formats can be most useful for

 
 
 
 

20. If I want to save a plot to a PDF file, which of the following is the correct way of doing that

 
 
 
 

MCQs Graph Plotting in R

  • Which of the following is an example of a valid graphics device in R?
  • Which of the following is the example of a vector graphics device in R?
  • Bitmapped file formats can be most useful for
  • Which of the following functions is typically used to add elements to a plot in the base graphics system?
  • Which function opens the screen graphics device on Windows?
  • What does the ‘pch’ option to par() control?
  • If I want to save a plot to a PDF file, which of the following is the correct way of doing that
  • Which function opens the screen graphics device for the Mac?
  • What does the gg in ggplot2 stand for?
  • Under the lattice graphics system, what do the primary plotting functions like xyplot() and bwplot() return?
  • Which of the following is a basic workhorse function of ggplot2?
  • Which types of the plot does qplot plot?
  • Transparency is determined by which parameter of the rgb function?
  • Which of the following is an R package that provides color palettes for sequential, categorical, and diverging data?
  • The following code does NOT result in a plot appearing on the screen device.
    library(lattice)
    library(datasets)
    data(airquality)
    p <- xyplot(Ozone ~ Wind | factor(Month), data = airquality)
    Which of the following is an explanation for why no plot appears?
  • In the lattice system, which of the following functions can be used to finely control the appearance of all lattice plots?
  • What is ggplot2 an implementation of?
  • What is a geom in the ggplot2 system?
  • When I run the following code I get an error: I was expecting a scatterplot of ‘votes’ and ‘rating’ to appear. What’s the problem?
  • The following code creates a scatterplot of ‘votes’ and ‘rating’ from the movies dataset in the ggplot2 package. After loading the ggplot2 package with the library() function, I can run qplot(votes, rating, data = movies)
    How can I modify the code above to add a smoother to the scatterplot?
Learn R Language and FAQS, MCQs Graph plotting in R

https://itfeature.com, https://gmstat.com

Matrix Multiplication in R: A Quick Tutorial

Introduction Matrix Multiplication in R

Matrix multiplication is a fundamental operation in linear algebra, and R provides efficient functions. The matrix multiplication in R can be done easily. For this purpose, the %*% operator is used for general matrix multiplication. An $n\times 1$ or $1 \times n$ vector (also called matrix) may be used as an $ n$ vector. In other words, vectors that occur in matrix multiplication expressions are automatically promoted to row (or column) vectors, whichever is multiplicatively coherent, if possible.

Scalar Multiplication

The * operator may be used for multiplying a matrix by a scalar quantity. The scalar value is multiplied by each element of the matrix.

m <- matrix(1:9, nrow = 3)
m <- 2 * m
m
Matrix Multiplication in R

From the above output, it can be seen that each element of the original matrix is multiplied by 2.

Element-wise Multiplication

If $A$ and $B$ are two square matrices of the same size, then the element-wise multiplication between matrices $A$ and $B$ can be performed using the * operator. In element-wise multiplication of the matrices, the corresponding elements of both matrices will be multiplied (provided that the matrices have the same dimension).

A <- matrix(1:9, nrow = 3)
A
## Ouput
     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9

B <- matrix(10:18, nrow = 3)
B

## Output
     [,1] [,2] [,3]
[1,]   10   13   16
[2,]   11   14   17
[3,]   12   15   18

A * B

## Output
     [,1] [,2] [,3]
[1,]   10   52  112
[2,]   22   70  136
[3,]   36   90  162

Matrix Multiplication in R

The matrix multiplication in R can be done easily. The general multiplication of matrices (matrix product) can be performed using the %*% operator. The matrix multiplication must satisfy the condition that the number of columns in the first matrix is equal to the number of rows in the second matrix. Suppose, if matrix $A$ has $m$ rows and $n$ columns and matrix $B$ has $n$ rows and $x$ columns, then the multiplication of these matrices will result in with dimension of $n times x$. Consider the following example of matrix multiplication in R language.

A <- matrix(1:9, nrow = 3)
B <- matrix(10:18, nrow = 3)

A %*% B
Matrix multiplication in R Language

Note the difference in output between A*B and A%*%B.

Suppose, $x$ is a vector, then the quadratic form of the matrices is

x <- c(5, 6, 7)
A <- matrix(1:9, nrow = 3)
x %% A %% x

## Output
     [,1]
[1,] 1764

Splitting the above multiplication procedure, one can easily understand how the matrices and vectors are multiplied.

x%*%A
## Output
[,1] [,2] [,3]
[1,]   38   92  146

x%*%A%*%x
## Output
     [,1]
[1,] 1764

The crossprod() in R

The function crossprod() forms “crossproducts” meaning that crossprod(X, y) is the same as t(X) %*% y. The crossprod() operation is more efficient than the t(X) %*%y.

crossprod(x, A)
     [,1] [,2] [,3]
[1,]   38   92  146

The cross product of $x$, $A$, the` (crossprod(x, A)) is equivalent to x%*%A, and crossprod(x%*%A, x) is equivalent to x%*%A%*%x.

Multiplication of Large Matrices

For larger matrices, the Matrix package may be used for optimized performance. The Matrix package also helps for working with sparse matrices or matrices with special structures.

Some Important Points about Matrices

  • Be careful about matrix dimensions to avoid errors.
  • Be careful about the use of operators * and %*%.
  • Be careful about the order of the matrices during multiplication (A%*%B, or B%*%A).
  • Explore other matrix operations like addition, subtraction, and transposition using R functions.
  • The dim() function helps identify the dimensions of a matrix.
  • For larger matrices, consider using the solve() function for matrix inversion or the eigen() function for eigenvalue decomposition.
Frequently Asked Questions About R

https://itfeature.com

https://gmstat.com

Special Values in R Programming: A Quick Guide

There are some special values in R Programming language, namely, these are NA, Inf, -inf, NaN, and NULL.

Special Values in R Programming Language

For numeric variables, several formalized special values are used. The calculations involving special values often result in special values. Regarding statistics, the real-world phenomenon should not include a special value. Therefore, it is desirable to handle special values before performing any statistical, especially inferential analysis. On the other hand, functions in R result in errors or warnings when a variable contains special values.

The NA values in R (NA stands for Not Available) represent the missing observations. A missing value may occur due to the non-response of the respondent or may arise when the vector size is expanded. For example,

v = c(1, 5, 6)
v[5] = 4
v

## Output
[1]  1  5  6 NA  4

To learn about how to handle missing values in R, see the article: Handling Missing Values in R

Inf and -Inf values in R represent a too-big number, which occurs during computation. Inf is for the positive number and -Inf is for the negative number (both represent the positive infinity, and negative infinity, respectively). Inf or -Inf also results when a value or variable is divided by 0. For example,

2 ^ 1024
## Output
[1] Inf

-2^1024

## Output
[1] -Inf

1/0

## Output
[1] Inf

-Inf + 1e10

## Output
[1] -Inf
Special Values in R programming Language

Sometimes a computation will produce a result that makes little sense. In such cases, R often returns NaN (Not a Number). For example,

Inf - Inf
NaN
0/0

## Output

In R, the Null object is represented by the symbol NULL. It is often used as an argument in functions to represent that no value was assigned to the argument. Additionally, some functions may return NULL. Note that the NULL is not the same as NA, Inf, -Inf, or NaN.

Getting Information about Special Values

Also, look at the str(), typeof(), and the length of Inf, -Inf, NA, NaN, and Null.

It is worth noting that, the special values in numeric variables indicate values that are not an element of the mathematical set of real numbers. One can use is.finite() function to determine whether the values are regular values or special values. is.finite() function only accepts vector objects. for example,

is.finite(c(1, Inf, NaN, NA))

A function can be written to deal with every numerical column in a data frame. For example,

special <- function(x){
    if (is.numeric(x)){
        return(!is.finite(x))
    }else {
        return (is.na(x))
    }
}

sapply(airquality, special)
Special values in R programming

The user defined special() function will test each column of the data frame object (airquality). The function will each special value if the object is numeric, otherwise it only checks for NA.

R FAQs: Special Values in R Programming

https://itfeature.com

https://gmstat.com