如何创建均值测试表和交叉表

时间:2019-02-17 18:04:34

标签: r

在过去的几天里,我一直在为此苦苦挣扎,我不会为您介绍各种失败的内容...

以下是我要回答的问题:

计算交叉列表,使您可以检验以下假设: •与公民进行比较,在2012年大选中,女性比男性更有投票权。

要回答此问题,请完成下面的表1。在括号中包括列百分比和单元格频率。在表格下方,包含解释测试结果的句子。

表1:2012年的性别和投票率

请参阅底部的链接,以获取我要创建的表的图片。

作为旁注,检查男性和女性的投票率。您认为受访者过度报告了投票率吗?

  1. 对均值进行比较,使您可以检验以下假设: •与公民相比,共和党人比民主党人对联邦政府的感觉更不利。

为此,请完成下面的表2。然后添加解释测试结果的句子。

表2:PID和联邦政府温度计的等级(三点PID刻度)

额外信用(2分),使用NES2012数据集中的两个变量构建假设。陈述您的假设,确定您的IV和DV,然后提供一张表格,该表格使用交叉表(如问题5)或均值比较(如问题6)来测试该假设。

第一个表:

This is the first table[1]

第二张表:

This is the second table[2]

1 个答案:

答案 0 :(得分:0)

读取数据

我下载了NES2012数据here,然后使用NES2012.sav封装读取了foreign

library(foreign)

data <- read.spss(file = "NES2012.sav",
                  to.data.frame = T,
                  use.value.labels = T)

确保使用值标签并将其作为data.frame读取

列队表

t1<-table(data$gender,data$voted2012)
print(t1)

控制台输出:

                   Did not vote Voted
-1. Inapplicable            0     0
Male                      537  2137
Female                    572  2267

我们对性别变量中的缺失值有一个级别,我们可以对数据集进行子集/重新编码,但是在这种情况下,我们可以简单地对表进行子集

t1 <- t1[2:3,] # pick the second to the third row and al the columns
print(t1)

控制台输出:

          Did not vote Voted
Male            537  2137
Female          572  2267

现在使用百分比,我们使用prop.table并相乘然后四舍五入

pt <- round(prop.table(t1) * 100,2)

您可以使用apply或cumsum或rowum添加页边距,而addmargins是最简单的页边距。

ptm <- addmargins(pt)

现在更改名称

colnames(ptm)[3] <- "Total" # only the third one
rownames(ptm)[3] <- "Total" # same thing

print(ptm)

控制台输出:

        Did not vote  Voted  Total
Male           9.74  38.76  48.50
Female        10.38  41.12  51.50
Total         20.12  79.88 100.00

平均值比较

我假设您所使用的测试是ANOVA,由于问题的水平,在我看来,这就像是SSCC中的统计学入门课程或定量方法。但是,如果不知道“联邦政府温度计等级”的测量范围,就不会推荐这种方法。只是为了解决任务,让我们继续吧!借助方差分析,我们可以在无效尼古丁病(H0 :)下研究均值之间没有差异。

  • H0:均值(X [Republican])==均值(X [Democrat])==均值(X [Independent])

  • H1:至少一种方法与其他方法不同

首先让我们用tapply获得简单明了的表,为此,我们必须准备清理并重新编码变量。 Ftgr_fedgov必须为数字,而pid_3必须为3级因子。 我将数字“ 1” ...“ 99”(缺少值等)的级别重新编码为NA,在我的情况下是levels()函数返回的前8个级别。请注意,由于我在第一行中读取spps .sav文件时指定了该因素,因此具有该因素。

levels(data$ftgr_fedgov)[1:8] <- NA

强制转换为数值

data$ftgt_fedgov <- as.numeric(data$ftgr_fedgov)  

使用带有ta的匿名函数来消除na.rm = T的NA

the.means <-tapply(data$ftgt_fedgov, data$pid_3, function(x) mean(x,na.rm = T))

meantab <- cbind(the.means)

colnames(meantab) <- "Mean Federal Gov rating"

print(meantab)

控制台输出:

     Mean Federal Gov rating
Dem           32.81931
Ind           23.73232
Rep           19.51978

现在放手进行方差分析:

myanova <-anova(lm(data = data, ftgt_fedgov ~ pid_3))

print(myanova)

控制台输出:

  Analysis of Variance Table

Response: ftgt_fedgov
Df  Sum Sq Mean Sq F value    Pr(>F)    
pid_3        2  164624   82312  342.58 < 2.2e-16 ***
  Residuals 5426 1303697     240                      
---
  Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

因此,我们有足够的证据拒绝空值,现在我们可以看看哪个组不同,有很多方法可以做到这一点,最常见的是Tukey。

myanova <- aov(data$ftgt_fedgov ~ data$pid_3)

TukeyHSD(myanova)

控制台输出:

  Tukey multiple comparisons of means
95% family-wise confidence level

Fit: aov(formula = data$ftgt_fedgov ~ data$pid_3)

$`data$pid_3`
            diff        lwr        upr     p adj
Ind-Dem  -9.086995 -10.217986  -7.956004     0
Rep-Dem -13.299528 -14.576857 -12.022198     0
Rep-Ind  -4.212533  -5.515013  -2.910053     0

在这种情况下,三组均值均不同(p adj)

您可以通过以下方式看到此信息:

par(mfcol = c(1,2))
boxplot(data$ftgt_fedgov ~ data$pid_3, main = "Federal Government Thermometer Rating",
        col = c("lightblue","lightgoldenrodyellow","pink"))
plot(TukeyHSD(myanova))

boxplot & tukey

此外,您将需要验证模型是否包含残差,变量子集的正态性等,但在我看来,赋值并不需要该模型。

额外分配

我的假设是,经常参加会议的人 宗教服务最不利于大麻合法化。

DV = pot_legal3
IV = relig_attend

两个变量都是因素,因此我在表中使用了chisq.test()

t1 <- table(data$relig_attend,data$pot_legal3)[,1:3] # removed weird column with [,1:3]

ht <- chisq.test() 

ht

控制台输出:

Pearson s Chi-squared test

data:  table(data$relig_attend, data$pot_legal3)[, 1:3]
X-squared = 470.58, df = 8, p-value < 2.2e-16

p值小于0.001,我们有足够的证据拒绝独立性的空洞化。

现在,让我们检查标准化残差以检查依赖性的方向。情节

library(ggplot2)

# chi & table into data.frame
df1 <- as.data.frame(t1)
df1$res <- as.vector(round(ht$stdres,2))
df1$exp <- as.vector(round(ht$expected,0))
df1$obv <- as.vector(ht$observed)

ggplot(df1,aes(Var1,Var2,fill = res, label = paste(df1$obv," / ",df1$exp,"\n",df1$res,sep = "")))+
  geom_raster()+
  scale_fill_gradient2(low = "blue", mid = "white",high = "red")+
  geom_text()+
  guides( fill = guide_colorbar(title.position = "top"))+
  scale_x_discrete(position = "top")+
  theme_minimal()+
  theme(axis.text.y  = element_text(angle = 90, size = 14, hjust = 0.5),
        axis.text.x = element_text(size = 12),
        plot.caption = element_text(face = "italic"),
        legend.position = "bottom",
        legend.direction = "horizontal",
        legend.key.height = unit(1,"mm"),
        legend.key.width = unit(14,"mm"),
        legend.title = element_text(size = 10),
        legend.title.align = 0.5,
        plot.title = element_text(hjust = 0.5,face = "bold.italic", size = 20, colour = "darkseagreen" )) +
  labs(title ="Pot & God",
       y = "Marihuana legalization" ,
       x = "Religious service attendance",
       fill = "Standarized residuals",
       caption  = "Data: SAGE NES2012     N: 5440",
       subtitle = "Observer / expected")

pot & god