R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

library(haven)
library(sjlabelled)
## 
## Attaching package: 'sjlabelled'
## The following objects are masked from 'package:haven':
## 
##     as_factor, read_sas, read_spss, read_stata, write_sas, zap_labels
library(tidyverse)
## -- Attaching packages ------------------------------------------ tidyverse 1.3.0 --
## √ ggplot2 3.3.2     √ purrr   0.3.4
## √ tibble  3.0.3     √ dplyr   1.0.2
## √ tidyr   1.1.2     √ stringr 1.4.0
## √ readr   1.3.1     √ forcats 0.5.0
## -- Conflicts --------------------------------------------- tidyverse_conflicts() --
## x forcats::as_factor()     masks sjlabelled::as_factor(), haven::as_factor()
## x dplyr::as_label()        masks sjlabelled::as_label()
## x dplyr::filter()          masks stats::filter()
## x dplyr::lag()             masks stats::lag()
## x sjlabelled::read_sas()   masks haven::read_sas()
## x sjlabelled::read_spss()  masks haven::read_spss()
## x sjlabelled::read_stata() masks haven::read_stata()
## x sjlabelled::write_sas()  masks haven::write_sas()
## x sjlabelled::zap_labels() masks haven::zap_labels()
library(sjmisc)
## 
## Attaching package: 'sjmisc'
## The following object is masked from 'package:purrr':
## 
##     is_empty
## The following object is masked from 'package:tidyr':
## 
##     replace_na
## The following object is masked from 'package:tibble':
## 
##     add_case
## The following objects are masked from 'package:sjlabelled':
## 
##     to_character, to_factor, to_label, to_numeric
library(car)
## Loading required package: carData
## 
## Attaching package: 'car'
## The following object is masked from 'package:dplyr':
## 
##     recode
## The following object is masked from 'package:purrr':
## 
##     some
library(gtsummary)
library(VIM)
## Loading required package: colorspace
## Loading required package: grid
## VIM is ready to use.
## Suggestions and bug-reports can be submitted at: https://github.com/statistikat/VIM/issues
## 
## Attaching package: 'VIM'
## The following object is masked from 'package:datasets':
## 
##     sleep
library(table1)
## 
## Attaching package: 'table1'
## The following objects are masked from 'package:base':
## 
##     units, units<-
library(ggplot2)
library(emmeans)
library(effects)
## Registered S3 methods overwritten by 'lme4':
##   method                          from
##   cooks.distance.influence.merMod car 
##   influence.merMod                car 
##   dfbeta.influence.merMod         car 
##   dfbetas.influence.merMod        car
## lattice theme set by effectsTheme()
## See ?effectsTheme for details.
library(ggeffects)
library(ggstatsplot)
## Registered S3 method overwritten by 'broom.mixed':
##   method      from 
##   tidy.gamlss broom
## In case you would like cite this package, cite it as:
##      Patil, I. (2018). ggstatsplot: "ggplot2" Based Plots with Statistical Details. CRAN.
##      Retrieved from https://cran.r-project.org/web/packages/ggstatsplot/index.html
library(modelsummary)
library(ordinal)
## 
## Attaching package: 'ordinal'
## The following object is masked from 'package:VIM':
## 
##     wine
## The following object is masked from 'package:dplyr':
## 
##     slice
cgss2017 <-read_dta("C:/Users/huawei/Desktop/R/cgss/cgss2017.dta")
cgss2017$age<-2017-cgss2017$a31
cgss2017[cgss2017 == 98] <- NA
cgss2017[cgss2017 == 9999999] <- NA
cgss2017[cgss2017 == 9999998] <- NA
cgss2017[cgss2017 == 9999997] <- NA
cgss2017[cgss2017 == 99] <- NA
cgss2017 <- sjlabelled::drop_labels(cgss2017)
table(cgss2017$c10)
## 
##    1    2    3    4    8 
##  101  708 2588  646  176
MY_CGSS <-
  cgss2017 %>% select(
    Pre_Sex = a38,
    gender = a2,
    nation = a4,
    age = age,
    edu = a7a,
    fair = c10,
    trust = v934,
    son = a68a1,
    daugther = a68a2,
  )

MY_CGSS[!complete.cases(MY_CGSS),]
## # A tibble: 8,486 x 9
##         Pre_Sex  gender  nation   age           edu   fair  trust   son daugther
##       <dbl+lbl> <dbl+l> <dbl+l> <dbl>     <dbl+lbl> <dbl+> <dbl+> <dbl>    <dbl>
##  1 2 [大多数情况下是不~  2 [女]  1 [汉]    80  9 [大学专科(成人高~     NA     NA     2        0
##  2 5 [完全是对的]~       1 [男]  1 [汉]    29  9 [大学专科(成人高~     NA     NA     0        0
##  3 3 [说不上对与不对]~   2 [女]  1 [汉]    59  4 [初中]                 NA     NA     1        0
##  4 4 [有时是对的]~       2 [女]  1 [汉]    48 12 [大学本科(正规高~     NA     NA     0        0
##  5 3 [说不上对与不对]~   2 [女]  1 [汉]    60  4 [初中]                 NA     NA     1        0
##  6 4 [有时是对的]~       1 [男]  1 [汉]    38 10 [大学专科(正规高~     NA     NA     0        1
##  7 3 [说不上对与不对]~   1 [男]  1 [汉]    25 11 [大学本科(成人高~     NA     NA     0        0
##  8 1 [总是不对的]~       2 [女]  1 [汉]    55 11 [大学本科(成人高~     NA     NA     1        0
##  9 2 [大多数情况下是不~  1 [男]  4 [回]    58  6 [普通高中]             NA     NA     1        0
## 10 4 [有时是对的]~       1 [男]  1 [汉]    20 10 [大学专科(正规高~     NA     NA    NA       NA
## # ... with 8,476 more rows
sum(is.na(MY_CGSS))
## [1] 17256
aggr(MY_CGSS,prop=F,numbers=T)

MY_CGSS$age <- as.numeric(MY_CGSS$age)
MY_CGSS$age <-
  car::recode(MY_CGSS$age,"18:30='1';31:44='2';45:59='3';60:103='4'")
table(MY_CGSS$age)
## 
##    1    2    3    4 
## 1848 2607 3755 4371
MY_CGSS$edu <- as.numeric(MY_CGSS$edu)
MY_CGSS$edu <-
  car::recode(MY_CGSS$edu, "9:13='1';5:8='2';1:4='3';'14'=NA")
table(MY_CGSS$edu)
## 
##    1    2    3 
## 2470 2235 7856
MY_CGSS$edu <-
  factor(MY_CGSS$edu,
         levels = c(1, 2, 3),
         labels = c("高", "中", "低"))
table(MY_CGSS$edu)
## 
##   高   中   低 
## 2470 2235 7856
MY_CGSS$Pre_Sex <- as.numeric(MY_CGSS$Pre_Sex)
MY_CGSS$Pre_Sex <-
  car::recode(MY_CGSS$Pre_Sex,"4:5='1';3='2';1:2='3'")


MY_CGSS$Pre_Sex <-
  factor(MY_CGSS$Pre_Sex,
         levels = c(1, 2, 3),
         labels = c("同意", "中立", "不同意"))

MY_CGSS$nation <- as.numeric(MY_CGSS$nation)
MY_CGSS$nation <-
  car::recode(MY_CGSS$nation,"1='1';2:8='2'")

MY_CGSS<-na.omit(MY_CGSS)

MY_CGSS<-filter(MY_CGSS,MY_CGSS$nation<2)

MY_CGSS$fair <- as.numeric(MY_CGSS$fair)
MY_CGSS$fair <-
  car::recode(MY_CGSS$fair,"1:2='1';3:4='2';8='3'")

MY_CGSS$fair <-
  factor(MY_CGSS$fair,
         levels = c(1, 2, 3),
         labels = c("占便宜", "公平", "矛盾"))

MY_CGSS$trust <- as.numeric(MY_CGSS$trust)
MY_CGSS$trust <-
  car::recode(MY_CGSS$trust,"1:2='1';3:4='2';8='3'")

MY_CGSS$trust <-
  factor(MY_CGSS$trust,
         levels = c(1, 2, 3),
         labels = c("信任", "谨慎", "矛盾"))

MY_CGSS$gender <-
  factor(MY_CGSS$gender,
         levels = c(1, 2),
         labels = c("男", "女"))
tbl_summary(MY_CGSS)
Characteristic N = 3,7811
Pre_Sex
同意 525 (14%)
中立 910 (24%)
不同意 2,346 (62%)
gender
1,820 (48%)
1,961 (52%)
nation 3,781 (100%)
age
1 521 (14%)
2 810 (21%)
3 1,122 (30%)
4 1,328 (35%)
edu
731 (19%)
707 (19%)
2,343 (62%)
fair
占便宜 724 (19%)
公平 2,914 (77%)
矛盾 143 (3.8%)
trust
信任 2,038 (54%)
谨慎 1,698 (45%)
矛盾 45 (1.2%)
[儿子]  请问您有几个亲生子女 (包括已去世子女)?
0 1,261 (33%)
1 1,772 (47%)
2 593 (16%)
3 110 (2.9%)
4 33 (0.9%)
5 11 (0.3%)
11 1 (<0.1%)
[女儿]  请问您有几个亲生子女 (包括已去世子女)?
0 1,749 (46%)
1 1,398 (37%)
2 465 (12%)
3 120 (3.2%)
4 38 (1.0%)
5 9 (0.2%)
26 1 (<0.1%)
60 1 (<0.1%)

1 Statistics presented: n (%)

MY_CGSS %>%
  select(gender,Pre_Sex,edu,age,fair,trust,son,daugther) %>% tbl_summary(
    by = Pre_Sex, 
    )
Characteristic 同意, N = 5251 中立, N = 9101 不同意, N = 2,3461
gender
295 (56%) 464 (51%) 1,061 (45%)
230 (44%) 446 (49%) 1,285 (55%)
edu
180 (34%) 268 (29%) 283 (12%)
111 (21%) 203 (22%) 393 (17%)
234 (45%) 439 (48%) 1,670 (71%)
age
1 169 (32%) 199 (22%) 153 (6.5%)
2 156 (30%) 259 (28%) 395 (17%)
3 115 (22%) 245 (27%) 762 (32%)
4 85 (16%) 207 (23%) 1,036 (44%)
fair
占便宜 96 (18%) 168 (18%) 460 (20%)
公平 410 (78%) 703 (77%) 1,801 (77%)
矛盾 19 (3.6%) 39 (4.3%) 85 (3.6%)
trust
信任 279 (53%) 471 (52%) 1,288 (55%)
谨慎 238 (45%) 429 (47%) 1,031 (44%)
矛盾 8 (1.5%) 10 (1.1%) 27 (1.2%)
[儿子]  请问您有几个亲生子女 (包括已去世子女)?
0 247 (47%) 359 (39%) 655 (28%)
1 218 (42%) 423 (46%) 1,131 (48%)
2 49 (9.3%) 102 (11%) 442 (19%)
3 7 (1.3%) 19 (2.1%) 84 (3.6%)
4 2 (0.4%) 5 (0.5%) 26 (1.1%)
5 2 (0.4%) 2 (0.2%) 7 (0.3%)
11 0 (0%) 0 (0%) 1 (<0.1%)
[女儿]  请问您有几个亲生子女 (包括已去世子女)?
0 314 (60%) 491 (54%) 944 (40%)
1 162 (31%) 326 (36%) 910 (39%)
2 35 (6.7%) 77 (8.5%) 353 (15%)
3 12 (2.3%) 12 (1.3%) 96 (4.1%)
4 1 (0.2%) 3 (0.3%) 34 (1.4%)
5 1 (0.2%) 1 (0.1%) 7 (0.3%)
26 0 (0%) 0 (0%) 1 (<0.1%)
60 0 (0%) 0 (0%) 1 (<0.1%)

1 Statistics presented: n (%)

table1(~ fair+trust+son+daugther+age+edu|gender, data=MY_CGSS)

(N=1820)

(N=1961)
Overall
(N=3781)
fair
占便宜 377 (20.7%) 347 (17.7%) 724 (19.1%)
公平 1377 (75.7%) 1537 (78.4%) 2914 (77.1%)
矛盾 66 (3.6%) 77 (3.9%) 143 (3.8%)
trust
信任 992 (54.5%) 1046 (53.3%) 2038 (53.9%)
谨慎 808 (44.4%) 890 (45.4%) 1698 (44.9%)
矛盾 20 (1.1%) 25 (1.3%) 45 (1.2%)
[儿子]  请问您有几个亲生子女 (包括已去世子女)?
Mean (SD) 0.880 (0.876) 0.961 (0.857) 0.922 (0.867)
Median [Min, Max] 1.00 [0, 11.0] 1.00 [0, 5.00] 1.00 [0, 11.0]
[女儿]  请问您有几个亲生子女 (包括已去世子女)?
Mean (SD) 0.740 (0.882) 0.828 (1.70) 0.786 (1.37)
Median [Min, Max] 1.00 [0, 5.00] 1.00 [0, 60.0] 1.00 [0, 60.0]
age
Mean (SD) 2.86 (1.05) 2.86 (1.04) 2.86 (1.05)
Median [Min, Max] 3.00 [1.00, 4.00] 3.00 [1.00, 4.00] 3.00 [1.00, 4.00]
edu
394 (21.6%) 337 (17.2%) 731 (19.3%)
366 (20.1%) 341 (17.4%) 707 (18.7%)
1060 (58.2%) 1283 (65.4%) 2343 (62.0%)
table1(~ Pre_Sex|son*daugther, data=MY_CGSS)
## Warning in table1.formula(~Pre_Sex | son * daugther, data = MY_CGSS): Terms
## to the right of '|' in formula 'x' define table columns and are expected to be
## factors with meaningful labels.
## Warning in .table1.internal(x = x, labels = labels, groupspan = groupspan, :
## Table has 43 columns. Are you sure this is what you want?
0
1
2
3
4
5
11
Overall
0
(N=476)
1
(N=573)
2
(N=170)
3
(N=30)
4
(N=8)
5
(N=3)
26
(N=1)
0
(N=884)
1
(N=616)
2
(N=192)
3
(N=60)
4
(N=15)
5
(N=5)
0
(N=332)
1
(N=151)
2
(N=80)
3
(N=21)
4
(N=9)
0
(N=45)
1
(N=38)
2
(N=19)
3
(N=4)
4
(N=3)
5
(N=1)
0
(N=9)
1
(N=14)
2
(N=3)
3
(N=4)
4
(N=3)
0
(N=3)
1
(N=5)
2
(N=1)
3
(N=1)
60
(N=1)
1
(N=1)
0
(N=1749)
1
(N=1398)
2
(N=465)
3
(N=120)
4
(N=38)
5
(N=9)
26
(N=1)
60
(N=1)
Pre_Sex
同意 151 (31.7%) 76 (13.3%) 17 (10.0%) 2 (6.7%) 0 (0%) 1 (33.3%) 0 (0%) 129 (14.6%) 68 (11.0%) 13 (6.8%) 7 (11.7%) 1 (6.7%) 0 (0%) 30 (9.0%) 13 (8.6%) 4 (5.0%) 2 (9.5%) 0 (0%) 3 (6.7%) 2 (5.3%) 1 (5.3%) 1 (25.0%) 0 (0%) 0 (0%) 0 (0%) 2 (14.3%) 0 (0%) 0 (0%) 0 (0%) 1 (33.3%) 1 (20.0%) 0 (0%) 0 (0%) 0 (0%) 0 (0%) 314 (18.0%) 162 (11.6%) 35 (7.5%) 12 (10.0%) 1 (2.6%) 1 (11.1%) 0 (0%) 0 (0%)
中立 156 (32.8%) 173 (30.2%) 26 (15.3%) 4 (13.3%) 0 (0%) 0 (0%) 0 (0%) 259 (29.3%) 121 (19.6%) 36 (18.8%) 4 (6.7%) 2 (13.3%) 1 (20.0%) 65 (19.6%) 23 (15.2%) 11 (13.8%) 3 (14.3%) 0 (0%) 9 (20.0%) 8 (21.1%) 2 (10.5%) 0 (0%) 0 (0%) 0 (0%) 2 (22.2%) 0 (0%) 1 (33.3%) 1 (25.0%) 1 (33.3%) 0 (0%) 1 (20.0%) 1 (100%) 0 (0%) 0 (0%) 0 (0%) 491 (28.1%) 326 (23.3%) 77 (16.6%) 12 (10.0%) 3 (7.9%) 1 (11.1%) 0 (0%) 0 (0%)
不同意 169 (35.5%) 324 (56.5%) 127 (74.7%) 24 (80.0%) 8 (100%) 2 (66.7%) 1 (100%) 496 (56.1%) 427 (69.3%) 143 (74.5%) 49 (81.7%) 12 (80.0%) 4 (80.0%) 237 (71.4%) 115 (76.2%) 65 (81.2%) 16 (76.2%) 9 (100%) 33 (73.3%) 28 (73.7%) 16 (84.2%) 3 (75.0%) 3 (100%) 1 (100%) 7 (77.8%) 12 (85.7%) 2 (66.7%) 3 (75.0%) 2 (66.7%) 2 (66.7%) 3 (60.0%) 0 (0%) 1 (100%) 1 (100%) 1 (100%) 944 (54.0%) 910 (65.1%) 353 (75.9%) 96 (80.0%) 34 (89.5%) 7 (77.8%) 1 (100%) 1 (100%)
table1(~ Pre_Sex|fair, data=MY_CGSS)
占便宜
(N=724)
公平
(N=2914)
矛盾
(N=143)
Overall
(N=3781)
Pre_Sex
同意 96 (13.3%) 410 (14.1%) 19 (13.3%) 525 (13.9%)
中立 168 (23.2%) 703 (24.1%) 39 (27.3%) 910 (24.1%)
不同意 460 (63.5%) 1801 (61.8%) 85 (59.4%) 2346 (62.0%)
table1(~ Pre_Sex|trust, data=MY_CGSS)
信任
(N=2038)
谨慎
(N=1698)
矛盾
(N=45)
Overall
(N=3781)
Pre_Sex
同意 279 (13.7%) 238 (14.0%) 8 (17.8%) 525 (13.9%)
中立 471 (23.1%) 429 (25.3%) 10 (22.2%) 910 (24.1%)
不同意 1288 (63.2%) 1031 (60.7%) 27 (60.0%) 2346 (62.0%)
ggstatsplot::ggpiestats(
  data = MY_CGSS,
  x = edu,
  y = gender,
  title = "教育与性别", # title for the plot
  legend.title = "性别", # title for the legend
  caption = substitute(paste(italic("Source"), ": 1974 Motor Trend US magazine"))
)

ggstatsplot::ggpiestats(
  data = MY_CGSS,
  x = edu,
  y = Pre_Sex,
  title = "教育与态度", # title for the plot
  legend.title = "态度", # title for the legend
  caption = substitute(paste(italic("Source"), ": 1974 Motor Trend US magazine"))
)

ggstatsplot::ggpiestats(
  data = MY_CGSS,
  x = trust,
  y = Pre_Sex,
  title = "信任程度与态度", # title for the plot
  legend.title = "态度", # title for the legend
  caption = substitute(paste(italic("Source"), ": 1974 Motor Trend US magazine"))
)

ggstatsplot::ggpiestats(
  data = MY_CGSS,
  x = fair,
  y = Pre_Sex,
  title = "公平与态度", # title for the plot
  legend.title = "态度", # title for the legend
  caption = substitute(paste(italic("Source"), ": 1974 Motor Trend US magazine"))
)

ggstatsplot::ggpiestats(
  data = MY_CGSS,
  x = age,
  y = Pre_Sex,
  title = "年龄与态度", # title for the plot
  legend.title = "态度", # title for the legend
  caption = substitute(paste(italic("Source"), ": 1974 Motor Trend US magazine"))
)

ggstatsplot::ggpiestats(
  data = MY_CGSS,
  x = son,
  y = Pre_Sex,
  title = "是否有儿子与态度", # title for the plot
  legend.title = "态度", # title for the legend
  caption = substitute(paste(italic("Source"), ": 1974 Motor Trend US magazine"))
)
## Warning in stats::chisq.test(x = x_arg, correct = FALSE): Chi-squared
## approximation may be incorrect

ggstatsplot::ggpiestats(
  data = MY_CGSS,
  x = daugther,
  y = Pre_Sex,
  title = "是否有女儿与态度", # title for the plot
  legend.title = "态度", # title for the legend
  caption = substitute(paste(italic("Source"), ": 1974 Motor Trend US magazine"))
)
## Warning in stats::chisq.test(x = x_arg, correct = FALSE): Chi-squared
## approximation may be incorrect

ggplot(MY_CGSS[is.na(MY_CGSS$Pre_Sex) == F,], aes(x=Pre_Sex,fill=edu)) +geom_bar(stat="count",position = "dodge")+geom_text(stat='count',aes(label=..count..),vjust=-1,position=position_dodge(width=0.9))

ggplot(MY_CGSS[is.na(MY_CGSS$Pre_Sex) == F,], aes(x=Pre_Sex,fill=fair)) +geom_bar(stat="count",position = "dodge")+geom_text(stat='count',aes(label=..count..),vjust=-1,position=position_dodge(width=0.9))

ggplot(MY_CGSS[is.na(MY_CGSS$Pre_Sex) == F,], aes(x=Pre_Sex,fill=trust)) +geom_bar(stat="count",position = "dodge")+geom_text(stat='count',aes(label=..count..),vjust=-1,position=position_dodge(width=0.9))

sat_mod1 <- lm(Pre_Sex ~ fair+edu, # regression formula
               data = MY_CGSS) # data 
## Warning in model.response(mf, "numeric"): using type = "numeric" with a factor
## response will be ignored
## Warning in Ops.factor(y, z$residuals): '-' not meaningful for factors
sat_mod2<-lm(Pre_Sex ~ fair+edu + gender, data = MY_CGSS) 
## Warning in model.response(mf, "numeric"): using type = "numeric" with a factor
## response will be ignored

## Warning in model.response(mf, "numeric"): '-' not meaningful for factors
sat_mod3<-lm(Pre_Sex ~ fair+edu + gender+age*edu*gender, data = MY_CGSS) 
## Warning in model.response(mf, "numeric"): using type = "numeric" with a factor
## response will be ignored

## Warning in model.response(mf, "numeric"): '-' not meaningful for factors
# model marginsplot ggeffects
mydf<-ggpredict(sat_mod3, terms = c("gender", "edu"),data = MY_CGSS)
## Warning in Ops.factor(r, 2): '^' not meaningful for factors
ggplot(mydf, aes(x = x, y = predicted, colour = group)) +
  geom_point() +
  stat_smooth(method = "lm", se = FALSE)
## `geom_smooth()` using formula 'y ~ x'

m3 <- clm(Pre_Sex ~ age+edu + gender+fair,
          data = MY_CGSS,na.action=na.omit)
m4 <- clm(Pre_Sex ~ age+edu + gender+fair+fair*edu ,
          data = MY_CGSS,na.action=na.omit)
modelsc <- list( m3,m4)
modelsummary(modelsc,stars = TRUE, coef_omit = "公平",stars_note = TRUE)
Model 1 Model 2
同意|中立 0.259** 0.329
(0.128) (0.201)
中立|不同意 1.766*** 1.837***
(0.131) (0.203)
age 0.595*** 0.595***
(0.036) (0.036)
edu中 0.351*** 0.267
(0.103) (0.253)
edu低 0.660*** 0.802***
(0.091) (0.210)
gender女 0.300*** 0.302***
(0.069) (0.069)
fair矛盾 -0.239 0.320
(0.191) (0.429)
edu中 × fair矛盾 -0.796
(0.562)
edu低 × fair矛盾 -0.605
(0.504)
Num.Obs. 3781 3781
AIC 6363.7 6366.5
BIC 6413.6 6441.3
Log.Lik. -3173.86128197866 -3171.2355492606
edf 8 12
* p < 0.1, ** p < 0.05, *** p < 0.01
??clm
## starting httpd help server ...
##  done