Question: Problems 2: 30 points You will use the data frame, chol, for this problem. Below is data from chol file: > head(chol) sex age chol
Problems 2: 30 points You will use the data frame, chol, for this problem.
Below is data from chol file:
> head(chol) sex age chol tg ht wt sbp dbp vldl hdl ldl bmi id.2 M 60 137 50 68.25 111.75 110 70 10 53 74 2.399066 id.3 M 26 154 202 82.75 184.75 88 64 34 31 92 2.698040 id.4 M 33 198 108 64.25 147.00 120 80 22 34 132 3.560993 id.5 F 27 154 47 63.25 129.00 110 76 9 57 88 3.224547 id.6 M 36 212 79 67.50 176.25 130 100 16 37 159 3.868313 id.7 F 31 197 90 64.50 121.00 122 78 18 58 111 2.908479 To test the correlation between two continuous variables, you can use the cor.test function. For example:
> age_chol <- cor.test(chol$age, chol$chol) > str(age_chol)
List of 9 $ statistic : Named num 3.13 ..- attr(*, "names")= chr "t" $ parameter : Named int 188 ..- attr(*, "names")= chr "df" $ p.value : num 0.00202 $ estimate : Named num 0.223 ..- attr(*, "names")= chr "cor" $ null.value : Named num 0 ..- attr(*, "names")= chr "correlation" $ alternative: chr "two.sided" $ method : chr "Pearson's product-moment correlation" $ data.name : chr "chol$age and chol$chol" $ conf.int : atomic [1:2] 0.0829 0.3538 ..- attr(*, "conf.level")= num 0.95 - attr(*, "class")= chr "htest" To extract the correlation coecient and the correponding p-value, you can write the following: > age_chol$estimate cor 0.2226466 > age_chol$p.value [1] 0.002018013
Write a function named myCorTest, which is used to calculate the pairwise correlation between one variable with a list of given variables. This function takes three arguments: dat : the name of the data frame, such chol. mainVar : a character vector of length 1 that contains the name of a continuous variable. For example: "wt". You will calculate the correlation between this variable with each of the variables in the third argument. varlist : a character vector contains one or more values. This argument contains the names of continuous variable.
The function will return a data frame that contains the correlation coecient and the corresponding p-value between each pair. For example, here are some sample results that are based on the myCortest function: > myCortest (chol, "wt", "age") var1 var2 R p age wt age 0.6660014 5.631448e-26 > myCortest (chol, "wt", c("age", "chol", "tg", "ht"))
var1 var2 R p age wt age 0.66600144 5.631448e-26 chol wt chol 0.06076105 4.049710e-01 tg wt tg 0.29461701 3.688497e-05 ht wt ht 0.85583311 2.705222e-56 > myCortest (chol, "bmi", c("sbp", "dbp", "vldl", "hdl", "ldl")) var1 var2 R p sbp bmi sbp 0.14927952 3.877523e-02 dbp bmi dbp 0.42636371 6.997094e-10 vldl bmi vldl 0.41033688 4.107925e-09 hdl bmi hdl -0.11984422 9.956239e-02 ldl bmi ldl 0.03449137 6.366170e-01
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
