关键词 > R代写 CS专业代写

HW1

发布时间:2020-01-19

Note: Please submit your homework in both paper and electronic copies. Please use Rmarkdown to produe your R results. This is due on 01/22/20(Wed)

(Question 1) 

Data input and manipulation.Open Rstudio and answer the . following questions. 

1. Run the code "rnorm(10,0,2)". Explain what your observe. Use "?rnorm" to understand this function. What does the input number 2 mean here? 

2. Run the code "sample(1:10,replace=FALSE)". Explain what you observe. What does replace do here? 

3. Create a 10x2 matrix:

v<-exp(rnorm(10,0,2))
p<-sample(1:10,replace=TRUE)
Mat<-cbind(p,v)
Mat<-data.frame(Mat)
rname<-c(1:10)
cname<-c("Time","Price")
dimnames(Mat)<-list(rname,cname)
Mat$Time

Mat$Price

4. Now you have your data. Transform it to data.frame by

Mat<-data.frame(Mat)

Next we sort the data by factor Time. Please read the slide titled "sorting" in the lecturenotes "2020STT461 W2M1.pdf". Please write the code to reorder "Mat" by "Time":

5. Let’s add one column "Average"to data "Mat". For each component of "Average", it should be Price divided by Time in the corresponding row. Please read the slide "Creating new variables" and write the code. Print new "Mat".

6. Since the data is now organized with right time order. Please do scatterplot in 2D for Time and Price in Mat(Use the function "Plot"). Let Price be predictor and Time be factor.

plot(Mat$Time,Mat$Price)

Please print the graph.

7. We want to improve our graph. We will label x and y axises by "Price" and "Time and add title "Price prediction" to our plot.

plot(Mat$TIme,Mat$Price,main="PriceVsTime"

,xlab="Time",ylab="Price")

Print the graph. 

(Question 2) 

1. Plot the graph in lines. Try the following code: plot(Mat$Time,Mat$Price,type="l") Intepret this code.
2. Scatterplot in 3D. Here we use function "scatterplot3d"

install.packages("scatterlot3d")

library("scatterplot3d")

scatterplot3d(Mat$Time,Mat$Price,Mat$Average)

3. Now name x,y,z axises by Time, Price and Average and add title "Scatterplot in 3D". Write the code and execute it.

(Question 3) 

1. Load the data "mtcars"

2. Run the following code. 

scatterplot3d(wt,disp,mpg,pch=16, 

highlight.3d=TRUE,type="h",main="3DScatterplot") 

Interpret "pch=16" and "highlight.3d=TRUE" in this code.

3. Plot scatterplot matrices among factors: mpg, cyl and wt.

4. Plot barplot with both factors gear and cyl under the title "Cars by gear and cylinder".

5. Plot boxplot of qsec by cyl with title "Speed test".