Re: [R] svykappa using the survey package
hi pradip, this should give you what you want library(foreign) library(survey) tf <- tempfile() download.file( " https://meps.ahrq.gov/mepsweb/data_files/pufs/h163ssp.zip; , tf , mode = 'wb' ) z <- unzip( tf , exdir = tempdir() ) x <- read.xport( z ) names( x ) <- tolower( names( x ) ) design <- svydesign(id=~varpsu,strat=~varstr, weights=~perwt13f, data=x, nest=TRUE) # include missings as "No" values here design <- update(design, xbpchek53 = ifelse(bpchek53 ==1,'yes','no or missing'), xcholck53 = ifelse(cholck53 ==1, 'yes','no or missing') ) # subset out records that were missing for either variable svykappa( ~ xbpchek53 + xcholck53 , subset(design, bpchek53 > 0 & cholck53 > 0 ) ) On Mon, Jun 20, 2016 at 7:49 PM, Muhuri, Pradip (AHRQ/CFACT) < pradip.muh...@ahrq.hhs.gov> wrote: > Hello, > > My goal is to calculate the weighted kappa measure of agreement between > two factors using the R survey package. I am getting the following error > message (the console is appended below; sorry no data provided). > > > # calculate survey Kappa > > svykappa(~xbpchek53+xcholck53, design) > Error in names(probs) <- nms : > 'names' attribute [15] must be the same length as the vector [8] > > I have followed the following major steps: > > 1) Used the "haven" package to read the sas data set into R. > 2) Used the dplyr mutate() to create 2 new variables and converted to > factors [required for the svykappa()?]. > 3) Created an object (named design) using the survey design variables and > the data file. > 4) Used the svykappa() to compute the kappa measure of agreement. > > I will appreciate if someone could give me hints on how to resolve the > issue. > > Thanks, > > Pradip Muhuri > > ### The detailed console is appended below > > > > setwd ("U:/A_PSAQ") > > library(haven) > > library(dplyr) > > library(survey) > > library(srvyr) > > library(Hmisc) > > my_hc2013_data <- read_sas("pc2013.sas7bdat") > > > > # Function to convert var names in upper cases to var names in lower > cases > > lower <- function (df) { > + names(df) <- tolower(names(df)) > + df > + } > > my_hc2013_data <- lower(my_hc2013_data) > > > > # Check the contents - Hmisc package (as above) required > > # contents(my_hc2013_data) > > > > # create two new variables > > my_hc2013_data <- mutate(my_hc2013_data, > + xbpchek53 = ifelse(bpchek53 ==1, 1, > + ifelse(bpchek53 %in% 2:6, 2,NA)), > + xcholck53 = ifelse(cholck53 ==1, 1, > +ifelse(cholck53 %in% 2:6, 2,NA))) > > > > # convert the numeric variables to factors for the kappa measure > > my_hc2013_data$xbpchek53 <- as.factor(my_hc2013_data$xbpchek53) > > my_hc2013_data$xcholck53 <- as.factor(my_hc2013_data$xcholck53) > > > > # check whether the variables are factors > > is.factor(my_hc2013_data$xbpchek53) > [1] TRUE > > is.factor(my_hc2013_data$xcholck53) > [1] TRUE > > > > > > # check the data from the cross table > > addmargins(with(my_hc2013_data, table(bpchek53,xbpchek53 ))) > xbpchek53 > bpchek53 1 2 Sum > -9 0 0 0 > -8 0 0 0 > -7 0 0 0 > -1 0 0 0 > 1 19778 0 19778 > 2 0 2652 2652 > 3 0 1014 1014 > 4 0 538 538 > 5 0 737 737 > 6 0 623 623 > Sum 19778 5564 25342 > > addmargins(with(my_hc2013_data, table(cholck53,xcholck53 ))) > xcholck53 > cholck53 1 2 Sum > -9 0 0 0 > -8 0 0 0 > -7 0 0 0 > -1 0 0 0 > 1 14850 0 14850 > 2 0 3153 3153 > 3 0 1170 1170 > 4 0 696 696 > 5 0 909 909 > 6 0 3764 3764 > Sum 14850 9692 24542 > > addmargins(with(my_hc2013_data, table(xbpchek53,xcholck53 ))) > xcholck53 > xbpchek53 1 2 Sum > 1 14667 4379 19046 > 2 163 5225 5388 > Sum 14830 9604 24434 > > > > # create an object with design variables and data > > design<-svydesign(id=~varpsu,strat=~varstr, weights=~perwt13f, > data=my_hc2013_data, nest=TRUE) > > > > # calculate survey Kappa > > svykappa(~xbpchek53+xcholck53, design) > Error in names(probs) <- nms : > 'names' attribute [15] must be the same length as the vector [8] > > # > > Pradip K. Muhuri, AHRQ/CFACT > 5600 Fishers Lane # 7N142A, Rockville, MD 20857 > Tel: 301-427-1564 > > > > > -Original Message- > From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Muhuri, > Pradip (AHRQ/CFACT) > Sent: Thursday, June 16, 2016 2:06 PM > To: David Winsemius > Cc: r-help@r-project.org > Subject: Re: [R] dplyr's arrange function - 3 solutions
[R] svykappa using the survey package
Hello, My goal is to calculate the weighted kappa measure of agreement between two factors using the R survey package. I am getting the following error message (the console is appended below; sorry no data provided). > # calculate survey Kappa > svykappa(~xbpchek53+xcholck53, design) Error in names(probs) <- nms : 'names' attribute [15] must be the same length as the vector [8] I have followed the following major steps: 1) Used the "haven" package to read the sas data set into R. 2) Used the dplyr mutate() to create 2 new variables and converted to factors [required for the svykappa()?]. 3) Created an object (named design) using the survey design variables and the data file. 4) Used the svykappa() to compute the kappa measure of agreement. I will appreciate if someone could give me hints on how to resolve the issue. Thanks, Pradip Muhuri ### The detailed console is appended below > setwd ("U:/A_PSAQ") > library(haven) > library(dplyr) > library(survey) > library(srvyr) > library(Hmisc) > my_hc2013_data <- read_sas("pc2013.sas7bdat") > > # Function to convert var names in upper cases to var names in lower cases > lower <- function (df) { + names(df) <- tolower(names(df)) + df + } > my_hc2013_data <- lower(my_hc2013_data) > > # Check the contents - Hmisc package (as above) required > # contents(my_hc2013_data) > > # create two new variables > my_hc2013_data <- mutate(my_hc2013_data, + xbpchek53 = ifelse(bpchek53 ==1, 1, + ifelse(bpchek53 %in% 2:6, 2,NA)), + xcholck53 = ifelse(cholck53 ==1, 1, +ifelse(cholck53 %in% 2:6, 2,NA))) > > # convert the numeric variables to factors for the kappa measure > my_hc2013_data$xbpchek53 <- as.factor(my_hc2013_data$xbpchek53) > my_hc2013_data$xcholck53 <- as.factor(my_hc2013_data$xcholck53) > > # check whether the variables are factors > is.factor(my_hc2013_data$xbpchek53) [1] TRUE > is.factor(my_hc2013_data$xcholck53) [1] TRUE > > > # check the data from the cross table > addmargins(with(my_hc2013_data, table(bpchek53,xbpchek53 ))) xbpchek53 bpchek53 1 2 Sum -9 0 0 0 -8 0 0 0 -7 0 0 0 -1 0 0 0 1 19778 0 19778 2 0 2652 2652 3 0 1014 1014 4 0 538 538 5 0 737 737 6 0 623 623 Sum 19778 5564 25342 > addmargins(with(my_hc2013_data, table(cholck53,xcholck53 ))) xcholck53 cholck53 1 2 Sum -9 0 0 0 -8 0 0 0 -7 0 0 0 -1 0 0 0 1 14850 0 14850 2 0 3153 3153 3 0 1170 1170 4 0 696 696 5 0 909 909 6 0 3764 3764 Sum 14850 9692 24542 > addmargins(with(my_hc2013_data, table(xbpchek53,xcholck53 ))) xcholck53 xbpchek53 1 2 Sum 1 14667 4379 19046 2 163 5225 5388 Sum 14830 9604 24434 > > # create an object with design variables and data > design<-svydesign(id=~varpsu,strat=~varstr, weights=~perwt13f, > data=my_hc2013_data, nest=TRUE) > > # calculate survey Kappa > svykappa(~xbpchek53+xcholck53, design) Error in names(probs) <- nms : 'names' attribute [15] must be the same length as the vector [8] # Pradip K. Muhuri, AHRQ/CFACT 5600 Fishers Lane # 7N142A, Rockville, MD 20857 Tel: 301-427-1564 -Original Message- From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Muhuri, Pradip (AHRQ/CFACT) Sent: Thursday, June 16, 2016 2:06 PM To: David Winsemius Cc: r-help@r-project.org Subject: Re: [R] dplyr's arrange function - 3 solutions received - 1 New Question Hello David, Your revisions to the earlier code have given me desired results. library("gtools") mydata[ mixedorder(mydata$prevalence_c, decreasing=TRUE), c("indicator", "prevalence_c") ] Thanks, Pradip Pradip K. Muhuri, AHRQ/CFACT 5600 Fishers Lane # 7N142A, Rockville, MD 20857 Tel: 301-427-1564 -Original Message- From: David Winsemius [mailto:dwinsem...@comcast.net] Sent: Thursday, June 16, 2016 12:54 PM To: Muhuri, Pradip (AHRQ/CFACT) Cc: r-help@r-project.org Subject: Re: [R] dplyr's arrange function - 3 solutions received - 1 New Question > On Jun 16, 2016, at 6:12 AM, Muhuri, Pradip (AHRQ/CFACT) >wrote: > > Hello, > > I got 3 solutions to my earlier code. Thanks to the contributors. May I > bring your attention to a new question below (with respect to David's > solution)? > > 1) Thanks to Daniel Nordlund for the tips - replacing leading space with a 0 > in the data. > > 2) Thanks to David Winsemius for his solution with the gtools::mixedorder > function. I have added an argument to his. > > mydata[