OnlineNursingOwl
← All nursing posts

Statistics · Studypool

using R assighment

Overview

Problem 1. Write your R codes (commands) for the following tasks and present your results clearly.

a. Generate a vector x with 30 random numbers from uniform distribution, i.e., U(0, 1). Show the values of x you get.

b. Use the R built-in functions (find the functions on your own if you don’t know them yet) to calculate the sample mean, median, and standard deviation of x.

c. Run the command y<- I(x > 0.5). Is y a numerical vector? Confirm or disapprove it by executing some appropriate R functions. d. Run the command z=cbind(x,y). Is z a matrix or data frame? Is the second column of z a logic vector or numerical vector? Show the output of summary(z).

e. What function can be used to change y into a character vector?

f. Run command xx=diag(x) in R. Is xx a vector or matrix? Find the length of xx by implementing length(xx) in R.

Problem 2. Use R to solve the following questions and present your results clearly.

a. Read data into R from the text file dataP2HW1.txt.

b. Calculate the sample mean and variance of X and Y separately.

c. Calculate Pearson’s correlation coefficient between X and Y .

d. Explore the function “lm” in R by checking “help(lm)” and then use it to fit a simple linear regression with Y being the dependent variable and X the independent variable. Report the fitted linear regression line.

e. Report the estimated regression coefficients in the form of a vector.

f. Create a character variable readme for “This is a simple linear regression problem.”

g. Create a list object (with name listP2HW1) that contains the data containing X and Y in the form of a data frame with 2 columns, the estimated regression coefficients in the form of a vector, the correlation coefficients, and readme. Assign the following names c("data", "coef", "corr", "readme") to the objects in listP2HW1. Display listP2HW1 and check out the command listP2HW1$Data$X