English 中文(简体)
anesrake - x+重量中的错误: 给二进制运算符的非数字参数
原标题:anesrake - Error in x + weights : non-numeric argument to binary operator
  • 时间:2023-07-17 15:17:01
  •  标签:
  • r

我试图通过anesrake 软件包计算样本重量,并不断收到上述错误,尽管多次试图排除故障。我正在用两个Vars(政党身份识别和政治意识形态)加权,直到我试图执行 anesrake 命令时,才得到错误。我尝试了目标变量的乘法,将其编码为数字,并确保没有NAs,没有运气。我无法附加数据文件,但我试图加权的数据格式低于数据,代码低于数据。

partyid7_ ideology
Democrat Liberal
Republican Conservative
Independent Moderate
#recoding/labeling target variables
df <- df %>% mutate(pid = case_when(partyid7_ %in% c(1,2) ~ "Democrat",
                                    partyid7_ %in% c(3,4,5) ~ "Independent",
                                    partyid7_ %in% c(6,7) ~ "Republican"),
                    ideol = case_when(ideology %in% c(1,2) ~ "Liberal",
                                      ideology == 3 ~ "Moderate",
                                      ideology %in% c(4,5) ~  
                                                "Conservative"))


# set target variable parameters  
ideol <- c("Liberal", "Moderate", "Conservative") 
ideo_prop <- c(0.25, 0.37, 0.36) #population proportions

pid <- c("Democrat", "Independent", "Republican") 
pid_prop <- c(0.28, 0.41, 0.28) #population parameters

population <- dplyr::data_frame(ideol, pid, ideo_prop, pid_prop) 
    # combining population parameters for target variables into one df

pop <- population %>% mutate(ideol = as.factor(ideol),
                             pid = as.factor(pid))

# create target list of variables to be weighted 
target <- with(pop, list(
  ideol = weights::wpct(ideol, ideo_prop),
  pid = weights::wpct(pid, pid_prop)
))

df$caseid <- 1:length(df$pid)

## creating anesrake object w/vector of weights per respondent
dfrake <- anesrake(target,                     
                   df,                          
                   caseid = df$caseid,               
                   cap = 3,                     
                   choosemethod = "total",     
                   type = "pctlim",            
                   pctlim = 0.05                
)
问题回答

您需要在运行 asrake 语法前转换数据框架。 请尝试 :

df$caseid <- 1:length(df$pid) # After this syntax,
df <-as.data.frame(df) # It s like anesrake does not support tibbles

# creating anesrake object w/vector of weights per respondent
dfrake <- anesrake(target,                     
                   df,                          
                   caseid = df$caseid,               
                   cap = 3,                     
                   choosemethod = "total",     
                   type = "pctlim",            
                   pctlim = 0.05                




相关问题
How to plot fitted model over observed time series

This is a really really simple question to which I seem to be entirely unable to get a solution. I would like to do a scatter plot of an observed time series in R, and over this I want to plot the ...

REvolution for R

since the latest Ubuntu release (karmic koala), I noticed that the internal R package advertises on start-up the REvolution package. It seems to be a library collection for high-performance matrix ...

R - capturing elements of R output into text files

I am trying to run an analysis by invoking R through the command line as follows: R --no-save < SampleProgram.R > SampleProgram.opt For example, consider the simple R program below: mydata =...

R statistical package: wrapping GOFrame objects

I m trying to generate GOFrame objects to generate a gene ontology mapping in R for unsupported organisms (see http://www.bioconductor.org/packages/release/bioc/vignettes/GOstats/inst/doc/...

Changing the order of dodged bars in ggplot2 barplot

I have a dataframe df.all and I m plotting it in a bar plot with ggplot2 using the code below. I d like to make it so that the order of the dodged bars is flipped. That is, so that the bars labeled "...

Strange error when using sparse matrices and glmnet

I m getting a weird error when training a glmnet regression. invalid class "dgCMatrix" object: length(Dimnames[[2]]) must match Dim[2] It only happens occasionally, and perhaps only under larger ...

Generating non-duplicate combination pairs in R

Sorry for the non-descriptive title but I don t know whether there s a word for what I m trying to achieve. Let s assume that I have a list of names of different classes like c( 1 , 2 , 3 , 4 ) ...

Per panel smoothing in ggplot2

I m plotting a group of curves, using facet in ggplot2. I d like to have a smoother applied to plots where there are enough points to smooth, but not on plots with very few points. In particular I d ...

热门标签