Using the R language:
Suppose we have a vector, called longvector with 1000 elements.
A. How would you let R calculate the mean value of longvector?
Write the code here:
B. What if there are some NA values and you'd like R to remove the
NA values when calculating the mean of longvector? Write the code
here:
C. How would you plot a histogram to show the distribution of
values in longvector? Write the code here:
D. How would you sample a random element from longvector? Write the
code here:
Code:
A)
#Declare vector
longvector <- 1:1000;
#calculate mean
mean(longvector)
B)
#Declare vector
longvector <- 1:1000;
#Last value made as NA
longvector[1000]=NA
#Calculate mean
mean(longvector,na.rm=TRUE)
C)
#Declare vector with random values
longvector <- runif(n=1000,min=1,max=100)
#histogram of longvector
hist(longvector)
#Declare vector with random values
longvector <- runif(n=1000,min=1,max=100)
#sample a random element
sample(longvector,1)
Using the R language: Suppose we have a vector, called longvector with 1000 elements. A. How...
3. Generate a vector u with 100 elements randomly selected (uniformly) from 1 to 40. Then generate a vector v with 100 elements randomly selected with a normal distribution of mean 20 and standard deviation 5 a) Write the vector to a file 'data.txt', using ,' as separator. (b) Remove the variable u (c) Scan the file and retrieve the variable u. (d) Print the 5-summary of the vector u (e) Plot the boxplot of u (f) Plot the histogram...
Using R, Exercise 4 (CLT Simulation) For this exercise we will simulate from the exponential distribution. If a random variable X has an exponential distribution with rate parameter A, the pdf of X can be written for z 2 0 Also recall, (a) This exercise relies heavily on generating random observations. To make this reproducible we will set a seed for the randomization. Alter the following code to make birthday store your birthday in the format yyyymmdd. For example, William...
4. Histograms Suppose we have a table called data with two numerical columns, "x" and·y. Consider the following scatter plot, which was generated by calling data.scatter('x', 'y') 2.0 1.5 1.0atos- 0.0 Histogram A: E 25 D 20 E 15 10 0 1 2 3 1 4 Histogram B: 40 C 30 20 a 10 0.0 0.5 1.0 1.5 .02.5 3.0 Question 1. One of these two lines of code generated Histogram A, and the other generated Histogram B . data.hist('x)...
What is the R code and result in Rstudio or R 1. Define a character vector in R as follows: y - c("a, "bc", "def") a. Report the output of the following function: length(y);y"a;y"b" b. nchar(x) takes a character vector x as an argument and returns a vector whose elements contain the sizes of the corresponding elements of x. Report the output of nchar(y) 2. Define a numeric vector in R as follows: z=c(3,7,1,2) a. Report the output of the...
Exercise 1: Sorting void mysort(vector & v); Write a function called mysort that rearranges elements of a vector v so that they form an increasing sequence of values. Allow for different vector positions to contain the same value. Develop your own sorting algorithm; do not use a predefined sort routine from the standard library. Implement a simple sorting algorithm rather than an efficient algorithm. Write code that thoroughly tests your function. Express your tests using assertions. Exercise 2: Comparison For...
5 Random Numbers and Histograms [Applied] Let x = x1 + ... + x20, the sum of 20 independent Uniform(0,1) random variables. In R, create 1,000 simulations of x and plot their histogram. On the histogram, overlay a graph of the normal density function with the same mean as x. Comment on any differences between the histogram and the curve. Hint 1: To plot a histogram in R you can build on the following code: library(ggplot2) df <- data.frame( x...
[MATLAB] Write a function called myMultProd.m that computes the cumulative product of the elements in a vector. The cumulative product, pj, of the jth element of the vector x, xj, is defined by pj = (x1)(x2) … (xj) for j = 1:length of the vector x. DO NOT USE CUMPROD For example, when you run your function it should look like this: >> x = [2 3 4 2]; >> myMultProd(x) >> ans = 2 6 24 48 That is,...
3. Write a function called sort3 that takes a 3-element vector as its sole arguments. It uses if-statements, possibly nested, to return the three elements of the vector as three scalar output-arguments in nondecreasino-roer , i.e., the first output argument equals the smallest element of the input vector and the last output argument equals the largest element. NOTE: Your function may NOT use any built-in functions, e.g., sort, min, max, median, etc. (5 points) (bonus question). Write a function called...
5. So far in our linear modeling, we have assumed that Ylz ~ NA,+Az,σ2); that is, there is a normal distribution of common variance around the regression line. Here, we change this up! Suppose that X~Unif(0, 1) and that for a given r, we know YlN(,22). (Here, the regression lne is 01z and the variance around the regression grows as r grows.) a. In R, figure out how to generate 1000 data points that follow this model and plot them....
Instructions First, download real estate data from the city of Ames, Iowa: download.file("http://www.openintro.org/stat/data/ames.RData", destfile = "ames.RData") load("ames.RData") Now write code in R to answer the following questions. Make sure the script that you turn in includes the code that you write, the output that you get from that code (in a comment), and a sentence or more answering the question if there was one (also in a comment). Consider the variable Gr.Liv.Area of the ames data frame, which represents the...