Question
Use R markdown for this assignment. Create markdown and html file. Principal Component Analysis Assignment (PCA) Write a function in R calledmyPCAwith the following properties:
Use R markdown for this assignment. Create markdown and html file.
Principal Component Analysis Assignment (PCA)
Write a function in R calledmyPCAwith the following properties:
1.The function must have 2 inputs:
a data frame that contains the data set
a scaler that denotes the desired percentage of variance preserved in the transformed data set
2.The function must have 3 outputs returned in a list, callednumber, PCA,andtransformed
the minimum number of principal components that is necessary to retain the desired variancespecied by the user
the principal components corresponding to previous part, in a matrix
the transformed data set in a matrix
Run your function on the rst 5 features of Rlongleydata set with the desired retained variance of 0.98.Longley data set is a data frame with 7 features related to economics.You will use the following features forthis assignment:GNP.deator, GNP, Unemployed, Armed.Forces, and Population.
It needs to look something like this: Function for PCATesting function with data set Longley
Importing and cleaning data set## 'data.frame': 16 obs. of 7 variables: ## $ GNP.deflator: num 83 88.5 88.2 89.5 96.2 ... ## $ GNP : num 234 259 258 285 329 ... ## $ Unemployed : num 236 232 368 335 210 ... ## $ Armed.Forces: num 159 146 162 165 310 ... ## $ Population : num 108 109 110 111 112 ... ## $ Year : int 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 ... ## $ Employed : num 60.3 61.1 60.2 61.2 63.2 ...
## $number ## [1] 3 ## ## $PCA ## [,1] [,2] [,3] ## [1,] -0.08251944 0.03443816 0.04192389 ## [2,] -0.75660491 0.31998587 0.55803783 ## [3,] -0.62690628 -0.57976739 -0.52027480 ## [4,] -0.15737310 0.74840481 -0.64418420 ## [5,] -0.05442221 0.01353004 0.03430775 ## ## $transformed ## 1947 1948 1949 1950 1951 1952 ## [1,] 186.63768 171.16153 84.57310 84.534362 106.029319 94.84982 ## [2,] -77.40974 -77.39434 -144.52821 -114.238901 81.237768 133.81495 ## [3,] 22.32844 46.86647 -34.78083 -4.842686 -7.962879 -20.98268 ## 1953 1954 1955 1956 1957 1958 ## [1,] 85.391818 -17.00751 3.841327 -4.792683 -29.26516 -137.75103 ## [2,] 139.830937 25.38542 52.910817 50.452892 47.13012 -65.41698 ## [3,] -4.368918 -81.72933 -7.945521 20.928929 32.18005 -47.09265 ## 1959 1960 1961 1962 ## [1,] -111.094025 -133.18865 -200.99653 -182.92335 ## [2,] -9.163444 -12.39969 -53.72050 23.50892 ## [3,] 24.960414 32.50811 -7.91433 37.84742
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started