代写MAST90083: Computational Statistics and Data Science Assignment 2代写R编程

MAST90083: Computational Statistics and Data Science

Assignment 2

Weight: 15%

Some details about Question 1 and 2

For both questions, use library ”HRW” that contains the ”WarsawApts” dataset. The sym-
bol n represents length of the variables for the given dataset (WarsawApts), and a bold 1
represents vector of ones. The mean squared error (MSE) is a normalized version of residual
sum of squares (RSS) and is given as MSE=RSS/n.
Notations
Throughout this assignment, small letter represents a value, small letter attached with a
subscript. represents an entry of a vector, small bold letter represents a vector, and capital
bold letter represents a matrix.
Question 1: Splines
The aim of this question is to learn how to code linear spline basis in order to approximate a
non-linear dataset via penalized spline regression (PSR). For PSR, a Cholesky decomposition
(CD) and singular value decomposition (SVD) approach can be used, but for this task the
regression based approach will be adopted, and any solution based on CD and SVD will not
be accepted. All the questions here are connected and must be attempted sequentially.
1. Store construction.date variable from WarsawApts in x and areaPerMzloty in y.
Generate 20 different location of knots and store them in vector k (ki corresponds
to i-th value of k, i = 1, ..., 20) using quantile (ref: Tutorial 5 Question 2) function.
To accomplish this task, you need a numeric vector of probabilities of length 22 that
can be generated using seq(0,1,length=22), however extreme values of 0 and 1 should
be excluded from this function in order to match the knots and the samples in x (x-
intercepts).
2. Using these knot locations now generate a Z matrix containing 20 linear spline basis
functions constructed using (x − ki)+. Also plot this matrix by limiting the range of
y-axis between -1 and 2 as shown in Figure 1a.
3. Using the vector of ones, x, and Z, construct a C matrix of size n × 22 (ref: lecture
slide 18 of the spline regression). Also, generate a D matrix of size 22× 22 consisting
of ones on the diagonal entries and zeros elsewhere except the first two entries (ref:
lecture slide 30 of the spline regression). Then, 100 values of tuning parameter (λ)
Figure 1: Spline basis and fitted curve
selected from 0 to 50 as seq(0,50,length=100) are used to estimate the fitted curves
using penalized spline regression.
4. Now, compute the RSS errors, degrees of freedom, and generalized cross validation
(GCV) associated with 100 tuning parameters.
5. For λ corresponding to minimum GCV, overlay the true plot with a fit of y as shown
in Figure 1b. As you can see from the plot that with 20 linear spline basis the fitted
curve looks a bit too linear and might not be a good fit. Lets increase the number
of basis to 60 and obtain Figure 1c (similar to 1a) and 1d (similar to 1b). Were the
results improved by increasing the number of basis? Compute the MSE to justify the
improvement/deterioration.
Question 2: Non-parametric Regression
The aim of this question is to learn how to code kernel regression using different kernels. Here,
question 2.1 and 2.2 are connected and must be attempted sequentially, whereas question
2.3, 2.4, and 2.5 are connected and must be attempted sequentially
1. Using 6 values of k selected from 3 to 23, write an R code for k-nearest-neighbor (NN)
averaging. You can do this by firstly estimating the absolute difference between the
current sample xi and the vector x as d = |xi1−x|, and secondly using d to find those
indices that correspond to its k smallest values. These indices are the indices of the k
NNs to the current xi. Use these indices to select the corresponding entries from the
data vector y and take their mean as your i-th estimate yˆi.
2. For each case of k, generate a figure illustrating a fitted vector yˆ on the true vector y (6
sub-figures in total, must be plotted in a single figure using 3×2 subplots), and compute
the MSE. For which case did you find the minimum MSE? In terms of MSE, what can
be said regarding the fitting comparison between k-NN and linear spline basis?
3. Using slide 13 of the kernel regression lecture, write a single function to accommodate
all 6 kernels. This function must be capable of returning 6 values, where each value
corresponds to the output value of a single kernel.
Figure 2: Cameraman picture and its density plot
4. Keeping the bandwidth fixed at h = 2 and using formula fˆ(xi) =
∑n
j=1Kh(xi,xj)yj∑n
j=1Kh(xi,xj)
,
(where {j = 1, ..., n}) code 6 kernel regression based fits for y by using 6 kernels from
the previous question ( To elaborate, for each sample xi and xj, six different kernel
values must be obtained in a single go to eventually generate six estimated values of
the i-th fit fˆ(xi), where fˆ(xi) is a vector and has 6 entries).
5. For each of the six kernels, generate a figure illustrating a fitted vector fˆ(x) on the true
vector y (6 sub-figures in total, must be plotted in a single figure using 3× 2 subplots).
Also, for each kernel estimate the MSE. For which kernel, the MSE is found to be
minimum?
Question 3: Expectation-Maximization
For this question, you are not allowed to use R’s built in function for the expectation-
maximization (EM) algorithm. You have already learnt how to code EM algorithm for
a gaussian mixture consisting of two probability density functions (pdfs), where both had
same standard deviation. Here, you will extend that algorithm to three cases, where all three
having different standard deviations.
1. In order to load relevant libraries and the provided image that is needed for the subse-
quent questions, use the following code
l i b r a r y (” p l o t . matrix ”)
l i b r a r y (”png”)
l i b r a r y (” f i e l d s ”)
I <= readPNG(”D:/ Code f i l e s /CM. png”)
I=I [ , , 1 ]
I=t ( apply ( I , 2 , rev ) )
par (mfrow=c (2 , 1 ) )
image ( I , c o l = gray ( ( 0 : 2 5 5 ) /255 ) )
p l o t ( dens i ty ( I ) )
2. The above code will generate figure 2 and as you can see from the density plot of the
cameraman picture (Figure 2b) that it is a mixture of three Gaussian distributions with
different standard deviations for each. Therefore, extend the EM algorithm so that it
can estimate parameters for these three pdfs. You may consider reshaping the image
data matrix I to one dimensional vector before applying your algorithm.
Figure 3: Cameraman picture labeling
3. You must terminate the algorithm when the stopping criteria (ej − ej−1) becomes less
than 10−6, where ej =
∑3
i=1(mi −m∗i ) +

(ci − c∗i ). Here, ej and ej−1 correspond to
the values from the current and previous iteration, respectively. Furthermore, mi and
ci represent i-th mean and standard deviation from the current iteration, respectively,
whereas m∗i and c

i represent i-th mean and standard deviation from the previous it-
eration. Also, use command ”print(round(c(m, c, p),4))” to print all 9 values in each
iteration, where pi (the i-th entry of vector p) is a mixing probability for the i-th distri-
bution and denoted as πˆ in the lecture slide 24 of the missing data and EM algorithm.
4. Using the results of EM algorithm, label the image pixels by using i) each pixel’s pdf
estimation (you must be able to read the assignment value from figure 3a, you may
want to use ”dnorm” function for this part), and ii) posterior pdfs multiplied with their
mean.
5. Plot both images obtained from the last question, and compare the results with Figure
3. You must obtain an exact match. You may want to use function ”image.plot” for
better visualization.







热门主题

课程名

mktg2509 csci 2600 38170 lng302 csse3010 phas3226 77938 arch1162 engn4536/engn6536 acx5903 comp151101 phl245 cse12 comp9312 stat3016/6016 phas0038 comp2140 6qqmb312 xjco3011 rest0005 ematm0051 5qqmn219 lubs5062m eee8155 cege0100 eap033 artd1109 mat246 etc3430 ecmm462 mis102 inft6800 ddes9903 comp6521 comp9517 comp3331/9331 comp4337 comp6008 comp9414 bu.231.790.81 man00150m csb352h math1041 eengm4100 isys1002 08 6057cem mktg3504 mthm036 mtrx1701 mth3241 eeee3086 cmp-7038b cmp-7000a ints4010 econ2151 infs5710 fins5516 fin3309 fins5510 gsoe9340 math2007 math2036 soee5010 mark3088 infs3605 elec9714 comp2271 ma214 comp2211 infs3604 600426 sit254 acct3091 bbt405 msin0116 com107/com113 mark5826 sit120 comp9021 eco2101 eeen40700 cs253 ece3114 ecmm447 chns3000 math377 itd102 comp9444 comp(2041|9044) econ0060 econ7230 mgt001371 ecs-323 cs6250 mgdi60012 mdia2012 comm221001 comm5000 ma1008 engl642 econ241 com333 math367 mis201 nbs-7041x meek16104 econ2003 comm1190 mbas902 comp-1027 dpst1091 comp7315 eppd1033 m06 ee3025 msci231 bb113/bbs1063 fc709 comp3425 comp9417 econ42915 cb9101 math1102e chme0017 fc307 mkt60104 5522usst litr1-uc6201.200 ee1102 cosc2803 math39512 omp9727 int2067/int5051 bsb151 mgt253 fc021 babs2202 mis2002s phya21 18-213 cege0012 mdia1002 math38032 mech5125 07 cisc102 mgx3110 cs240 11175 fin3020s eco3420 ictten622 comp9727 cpt111 de114102d mgm320h5s bafi1019 math21112 efim20036 mn-3503 fins5568 110.807 bcpm000028 info6030 bma0092 bcpm0054 math20212 ce335 cs365 cenv6141 ftec5580 math2010 ec3450 comm1170 ecmt1010 csci-ua.0480-003 econ12-200 ib3960 ectb60h3f cs247—assignment tk3163 ics3u ib3j80 comp20008 comp9334 eppd1063 acct2343 cct109 isys1055/3412 math350-real math2014 eec180 stat141b econ2101 msinm014/msing014/msing014b fit2004 comp643 bu1002 cm2030
联系我们
EMail: 99515681@qq.com
QQ: 99515681
留学生作业帮-留学生的知心伴侣!
工作时间:08:00-21:00
python代写
微信客服:codinghelp
站长地图