Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Worksheet 2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Winter 2023 NCKU Modular Course

Computer Experiments and Uncertainty Quantification


Worksheet 2
Due: 5 pm of Jan 11 2023
Consider the following model, which is denoted by model (1):
𝑦 = 𝑓(𝑥) + 𝑒rror,
𝑓(𝑥) = 10 + 10 sin(4𝜋𝑥 0.9) 𝑒 −1.5𝑥 , (1)
𝑒rror ~ 𝑁(0, 1)

I. Generate a dataset
a. Generate 11 equally spaced points between 0 and 1 and assign
the resulting vector as a variable x_n11. Please type set.seed(2)
before you start writing your results.

b. Write a function for 𝑓(𝑥 ) and name the function by CE_function1.

c. Generate 11 random error values from normal distribution with


mean 0 and variance 1 and save it as a vector with name
error_n11.

d. Use results from a and c to generate 11 values as a vector for y


and assign the results as a variable y_n11.

e. Create a dataframe which includes two columns x_n11 and y_n11


and assign the resulting dataframe as a variable data_n11 .

II. Visualize the data


f. b. Plot y_n11 versus x_n11. Set the title as “Scatter Plot 1”. (Hint:
Check function plot in R )

III. Fit the model


g. Fit a polynomial regression model to the dataset data_n11 by
using the following code

#sample R code for question f


lm_n11 <- lm(y_n11 ~ poly(x_n11, 3), data_n11)
Hint: (1) Please check function lm and poly in R and (2) The example
fits a polynomial regression with degrees 3.

h. Try summary(lm_n11) and describe what you observed.

i. Plot the residuals (observed value – fitted value) versus x_n11?


What do you observe? Hint: the fitted values can be generated by
predict(lm_n11, data_n11)

j. Plot the residuals versus the fitted values.

k. Report the R square values and adjusted R square values of


lm_n11 in question (g) by using the following code:
summary(lm_n11)$r.squared
summary(lm_n11)$adj.r.squared

Check google and then provide your interpretation the r.squared


value and the adj.r.squared value you reported.

IV. Assess Prediction Error


l. Generate a testing dataset with 20 data points from model (1) and
write a for loop in R that can assess the prediction errors of the
polynomial regression model in (g) with degrees 1, 2, 3, 4, 5, 6.
Save the prediction errors in a list format.

Guideline for the worksheet


1. Please use R markdown to generate one pdf file as your worksheet
results and submit it to Moodle, so you can include your R code.
Note: It is not necessary to type the question in your results for the
submission.
2. Please remember to type the question number with your answer and
R code.

You might also like