Home › Forums › Default Forum › Problem with page 236 Discovering Statistics using R
- This topic has 3 replies, 2 voices, and was last updated 8 years, 7 months ago by
Kieran Duignan.
-
AuthorPosts
-
26th June 2012 at 12:56 pm #2298
Kieran Duignan
MemberTo carry out a pcor command, as prescribed on page 236 of Discovering Statistics using R, I input the command as set out in the text.
An error message came back stating that pcor cannot be found.
This was after I called the ggm package as prescribed on page 236 which provided this response
> library(ggm)
Loading required package: gRbase
Loading required package: MASS
Loading required package: graph
Error: package ‘graph’ could not be loaded
In addition: Warning messages:
1: package ‘ggm’ was built under R version 2.15.1
2: package ‘gRbase’ was built under R version 2.15.1
3: In library(pkg, character.only = TRUE, logical.return = TRUE, lib.loc = lib.loc) :
there is no package called ‘graph’A respondent on the Linked In R Programming Group observed that ggm won’t work with the latest version of R (2.15.1).
Does this effectively mean that there is no way of calculating partial correlation using R? Or is there an alternative command sequence to the one presented on page 236 of Discovering Statistic using R that does the job?
27th June 2012 at 6:24 am #2301Kieran Duignan
MemberThis morning, I downloaded the ‘ggm’ package again, this time with the ‘Dependencies=TRUE’ statement in the command.
The feedback information stated that the RGBL extension is not available, which seems to explain why the ‘pcor’ command no longer works within the ggm package.
Right?
27th June 2012 at 9:04 am #2300Anonymous
Inactiveit seems like the RBGL package has been deprecated (it is no longer available in the CRAN) which means that ggm which uses gRbase and graph that both rely somewhat in RBGL are creating all kinds of funky errors…
…. but not a problem, there’s always a way to go around stuff like this in R. if you’re still looking around for ways to calculate the partial correlation may i suggest you use the R package ‘ppcor’? it has the pcor() function that you’re looking for.
let’s see an example! (just copy-paste this code on your R script and it should work. i tried to write very
detailed explanations)
#THESE LOADS THE LIBRARIES WE ARE GOING TO USE
library(MASS) library(ppcor)
#VERY QUICK FUNCTION TO GENERATE A 4-DIMENSIONAL NORMAL DISTRIBUTION. NOTICE HOW ALL #THE ELEMENTS INSIDE THE FUNCTION matrix() HAVE 0.5s ON THEM. THAT MEANS THAT THESE 4 #VARIABLES WILL HAVE A CORRELATION AMONG THEMSELVES OF 0.5 GIVE OR TAKE BECAUSE R #WILL ADD RANDOM ERROR TO THEM SO THEY WILL NOT BE EXACTLY 0.5. MY SAMPLE SIZE IS 1000 #AND THE MEANS OF THE VARIABLES ARE 0.
A <- {mvrnorm(1000, c(0,0,0,0), matrix(c(1.0,0.5,0.5,0.5, 0.5,1.0,0.5,0.5, 0.5,0.5,1.0,0.5, 0.5,0.5,0.5,1.0), nrow=4, byrow=T))}
#IN THE REAL WOLRD MY DATASET WOULD NOW BE ‘A’
#NOW I AM GOING TO USE THE pcor() FUNCTION TO OBTAIN THE MATRIX OF PARTIAL #CORRELATIONS
pcor(A, method=c(“pearson”))
#THE RESULTING MATRIX IS A MATRIX WHERE ALL THE VARIABLES HAVE BEEN PARTIALLED-OUT #FROM ALL THE OTHER ONES. NOTICE HOW THEY ARE ALL MORE OR LESS THE SAME NUMBERS #BECAUSE THEY WERE INITIALLY CORRELATED MORE OR LESS THE SAME
within the ‘ppcor’ package there seems to be quite a bit of flexibility to calculate the partial and semipartial correlations. nevertheless, other packages like the ‘psych’ or ‘relaimpo’ packages either have built-in functions that calcualte them or give you the necessary information (i.e. R-square change) to calculate them yourself. or you can code your own function (i think i have one for the 2-variable case only lying around if you’d like to see it).
quoting the lovely Simon Blomberg for cases like this:
“This is R. There is no if. Only how”
have fun!
27th June 2012 at 9:27 am #2299Kieran Duignan
MemberMuch appreciated, Oscar.
While I’m not familiar with the ‘lovely Simon Blomberg’, I share the sentiment expressed.
Cheerio
-
AuthorPosts
- The forum ‘Default Forum’ is closed to new topics and replies.