Skip to content

Commit

Permalink
See comments on github
Browse files Browse the repository at this point in the history
  • Loading branch information
aiorazabala committed Feb 25, 2021
1 parent 9be328c commit fa26315
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 43 deletions.
3 changes: 1 addition & 2 deletions R/qflag.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ qflag <- function(loa=loa, nstat) {
# -- 2) qsorts which square loading is higher than the sum of square loadings of the same q-sort in all other factors
thold.05 <- 1.96/sqrt(nstat)
loa_sq <- loa^2
flagged <- data.frame(cbind(1:nqsorts))
flagged[,1] <- as.logical(flagged[,1])
flagged <- matrix(data=F, nrow=nqsorts, ncol=ncol(loa))
f <- 1
while (f <= ncol(loa)) {
n <- 1
Expand Down
55 changes: 28 additions & 27 deletions R/qmethod.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,44 +28,45 @@ qmethod <- function(dataset, nfactors, extraction="PCA", rotation="varimax", for
# Run the analysis
cor.data <- cor(dataset, method=cor.method)
if(extraction == "PCA") {
loa <- as.data.frame(unclass(principal(cor.data, nfactors=nfactors, rotate=rotation, ...)$loadings)) #PCA from {psych} for factor loadings
loa <- unclass(principal(cor.data, nfactors=nfactors, rotate=rotation, ...)$loadings) #PCA from {psych} for factor loadings
}
if(extraction == "centroid") {
loa <- as.data.frame(unclass(centroid(tmat=cor.data, nfactors=nfactors, spc)))
loa.unr <- unclass(centroid(tmat=cor.data, nfactors=nfactors, spc))
loa <- unclass(varimax(loa.unr[,1:nfactors])$loadings)
}
names(loa) <- paste0("f", 1:length(loa))
# The following depends on the qmethod functions: qflag, qzscores, qfcharact, qdc
flagged <- qflag(loa=loa, nstat=nstat)
qmethodresults <- qzscores(dataset, nfactors, flagged=flagged, loa=loa, forced=forced, distribution=distribution)
if(extraction == "PCA") qmethodresults$brief$extraction <- extraction
if(extraction == "centroid") qmethodresults$brief$extraction <- paste0(extraction, " (threshold = ", spc, ")")
qmethodresults$brief$rotation <- rotation
qmethodresults$brief$flagging <- "automatic"
qmethodresults$brief$cor.method <- cor.method
qmethodresults$brief$rotation <- rotation
qmethodresults$brief$flagging <- "automatic"
qmethodresults$brief$cor.method <- cor.method
qmethodresults$brief$pkg.version <- packageVersion('qmethod')
qmethodresults$brief$info <- c("Q-method analysis.",
paste0("Finished on: ",
qmethodresults$brief$date),
paste0("'qmethod' package version: ",
qmethodresults$brief$pkg.version),
paste0("Original data: ",
qmethodresults$brief$nstat,
" statements, ",
qmethodresults$brief$nqsorts, " Q-sorts"),
paste0("Forced distribution: ",
qmethodresults$brief$distro),
paste0("Number of factors: ",
qmethodresults$brief$nfactors),
paste0("Extraction: ",
qmethodresults$brief$extraction),
paste0("Rotation: ",
qmethodresults$brief$rotation),
paste0("Flagging: ",
qmethodresults$brief$flagging),
paste0("Correlation coefficient: ",
qmethodresults$brief$cor.method))
qmethodresults$brief$info <- c("Q-method analysis.",
paste0("Finished on: ",
qmethodresults$brief$date),
paste0("'qmethod' package version: ",
qmethodresults$brief$pkg.version),
paste0("Original data: ",
qmethodresults$brief$nstat,
" statements, ",
qmethodresults$brief$nqsorts, " Q-sorts"),
paste0("Forced distribution: ",
qmethodresults$brief$distro),
paste0("Number of factors: ",
qmethodresults$brief$nfactors),
paste0("Extraction: ",
qmethodresults$brief$extraction),
paste0("Rotation: ",
qmethodresults$brief$rotation),
paste0("Flagging: ",
qmethodresults$brief$flagging),
paste0("Correlation coefficient: ",
qmethodresults$brief$cor.method))
qmethodresults[[8]] <- qdc(dataset, nfactors, zsc=qmethodresults$zsc,
sed=as.data.frame(qmethodresults$f_char$sd_dif))
sed=qmethodresults$f_char$sd_dif)
names(qmethodresults)[8] <- "qdc"
if (silent== FALSE) cat(qmethodresults$brief$info, sep="\n")
return(qmethodresults)
Expand Down
18 changes: 11 additions & 7 deletions R/qzscores.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,22 @@ qzscores <- function(dataset, nfactors, loa, flagged, forced = TRUE, distributio
f_char <- qfcharact(loa, flagged, zsc, nfactors, floa)
#F. FINAL OUTPUTS
brief <- list()
brief$date <- date()
brief$nstat <- nstat
brief$nqsorts <- nqsorts
brief$distro <- forced
brief$nfactors <- nfactors
brief$rotation <- "Unknown: loadings were provided separately."
brief$cor.method <- "Unknown: loadings were provided separately."
brief$date <- date()
brief$pkg.version <- packageVersion('qmethod')
brief$nstat <- nstat
brief$nqsorts <- nqsorts
brief$distro <- forced
brief$nfactors <- nfactors
brief$extraction <- "Unknown: loadings were provided separately."
brief$rotation <- "Unknown: loadings were provided separately."
brief$cor.method <- "Unknown: loadings were provided separately."
brief$info <- c("Q-method z-scores.",
paste0("Finished on: ", brief$date),
paste0("'qmethod' package version: ", brief$pkg.version),
paste0("Original data: ", brief$nstat, " statements, ", brief$nqsorts, " Q-sorts"),
paste0("Forced distribution: ", brief$distro),
paste0("Number of factors: ", brief$nfactors),
paste0("Extraction: ", brief$extraction),
paste0("Rotation: ", brief$rotation),
paste0("Flagging: Unknown: flagged Q-sorts were provided separately."),
paste0("Correlation coefficient: ", brief$cor.method))
Expand Down
38 changes: 31 additions & 7 deletions man/centroid.Rd
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
\name{centroid}
\alias{centroid}
\title{Q methodology: centroid extraction}
\description{Extracts factors using the centroid approach.}
\description{Extracts factors/ components using the centroid approach as an alternative to Principal Components Analysis.}
\usage{centroid(tmat, nfactors = 7, spc)}

\arguments{
\item{tmat}{a correlation matrix between Q-sorts.}
\item{nfactors}{number of factors/ components to extract.}
\item{nfactors}{number of factors/ components to extract. Defaults to 7.}
\item{spc}{the threshold to accept factor results, set to 0.005 by default (in Brown 1980, this is set to 0.02).}
}

\details{In order to implement manual flagging, use a manually created data frame for \code{flagged}. See an example of code to perform manual flagging or to manipulate the loadings in \url{https://github.com/aiorazabala/qmethod/wiki/Advanced-analysis}.
\details{This functions implement the centroid method for extraction of factors, an alternative to Principal Components that can be used in Q methodology. The calculations are based in Brown (1980; below).

The loadings from \code{principal(...)$loadings} can be explored to decide upon flagging. The \code{loa} data frame should have Q-sorts as rows, and factors as columns, where \code{TRUE} are the flagged Q-sorts.}
The function is called from within \code{\link{qmethod}} where the attribute \code{extraction} is set to \code{centroid}.

This function can be used independently where conducting each step of the analysis separately, preceded by a correlation between Q-sorts and followed by the rotation of factors/ components (see below), calculation of z-scores, etc.
}
\value{
Returns a matrix with Q-sorts as rows, and rotated factors as columns
Returns a matrix with Q-sorts as rows, and rotated factors as columns.
}
\references{Brown, S. R., 1980 \emph{Political subjectivity: Applications of Q methodology in political science}, New Haven, CT: Yale University Press, pages 208-224.

Expand All @@ -29,13 +32,34 @@ See further references on the methodology in \code{\link{qmethod-package}}.}
require('qmethod')
require ("psych")

# Load data
data("lipset")
lip <- lipset[[1]]

# Correlation matrix
corlip <-cor(lip)

# Centroid extraction
lipcent <- centroid(corlip)
lipcent

# varimax rotation over 3 factors

## To finalise the full analysis, continue with the following steps
# Rotation (in this example, varimax over 3 factors)
vmax <- varimax(lipcent[,1:3])
qflag (unclass(vmax$loadings), nstat = 33)}

# Automatic pre-flagging of Q-sorts
flags <- qflag(unclass(vmax$loadings), nstat = 33)

# Calculate z-scores and general characeristics
results <- qzscores(lip, 3, loa=vmax$loadings, flagged=flags)
summary(results)

# Consensus and distinguishing statements
results$qdc <- qdc(lip, 3, zsc=results$zsc, sed=as.data.frame(results$f_char$sd_dif))

plot(results)

## All of the above can be done with:
results2 <- qmethod(lip, 3, extraction="centroid")
}

2 comments on commit fa26315

@aiorazabala
Copy link
Owner Author

@aiorazabala aiorazabala commented on fa26315 Feb 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Changed from data.frame to matrix (issue simplify / streamline data types #277):
    • loadings,
    • flagged,
    • general characteristics:SD,
  • Corrected implementation of centroid() in qmethod()
  • Improved documentation of centroid() and enhanced example to become a complete Q analysis
  • Added pkg version and extraction method to output of qzscores()

@aiorazabala
Copy link
Owner Author

@aiorazabala aiorazabala commented on fa26315 Mar 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.