a) Write the R-commands for generating random variables from normal distribution
a) Write the R-commands for generating 800 random variables from normal distribution by using the following information. Mean = 10, Standard deviation = 4, n=5 and k=2500.
b) Simulate a coin toss for 30 times using R-commands." solve it in detail
Solution:
a) To generate 800 random variables from a normal distribution with a mean of 10, standard deviation of 4, n of 5, and k of 2500, you can use the following R commands:
set.seed(123) # Set a seed for reproducibility (optional)
# Generate random variables
random_vars <- rnorm(n = 800, mean = 10, sd = 4)
# Reshape the vector into a matrix of dimensions (n x k)
matrix_vars <- matrix(random_vars, nrow = 5, ncol = 160)
# Calculate the row means
row_means <- rowMeans(matrix_vars)
# Calculate the column means
col_means <- colMeans(matrix_vars)
In the above code, rnorm() is used to generate 800 random variables from a normal distribution with the specified mean (10) and standard deviation (4). The set.seed() function is optional but allows you to reproduce the same set of random numbers.
The matrix() function reshapes the vector of random variables into a matrix with dimensions (n x k), where n is the number of rows (5) and k is the number of columns (160).
Finally, rowMeans() and colMeans() are used to calculate the row means and column means, respectively, of the generated matrix.
b) To simulate a coin toss for 30 times using R commands, you can use the following code:
set.seed(456) # Set a seed for reproducibility (optional)
# Simulate coin toss
coin_toss <- sample(c("Heads", "Tails"), size = 30, replace = TRUE)
# Print the results
print(coin_toss)
In the above code, sample() is used to randomly select between "Heads" and "Tails" for 30 times (size = 30) with replacement (replace = TRUE). The set.seed() function is optional but allows you to reproduce the same sequence of coin tosses.
Finally, the print() function is used to display the simulated coin toss results.
No comments