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
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(sjmisc) library(sjlabelled) library(tidyverse) library(car) library(gtsummary) library(VIM) library(table1) cgss2017 <- read_dta(“C:/Users/huawei/Desktop/R/cgss”)
c2017 <- sjmisc::to_label(cgss2017)
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)
MY_CGSS[!complete.cases(MY_CGSS),] sum(is.na(MY_CGSS)) aggr(MY_CGSS,prop=F,numbers=T)
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\(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)
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)
MY_CGSS\(edu <- factor(MY_CGSS\)edu, levels = c(1, 2, 3), labels = c(“高”, “中”, “低”)) table(MY_CGSS$edu)
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)
MY_CGSS %>% select(gender,Pre_Sex,edu,age,fair,trust,son,daugther) %>% tbl_summary( by = Pre_Sex, )
table1(~ fair+trust+son+daugther+age+edu|gender, data=MY_CGSS)
table1(~ Pre_Sex|son*daugther, data=MY_CGSS)
table1(~ Pre_Sex|fair, data=MY_CGSS)
table1(~ Pre_Sex|trust, data=MY_CGSS)
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”)) )
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”)) )
ggplot(MY_CGSS[is.na(MY_CGSS$Pre_Sex) == F,], aes(x=Pre_Sex)) + geom_bar(stat=“count”,position = “dodge”,na.rm = TRUE)+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=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))
library(emmeans) #emmeans library(ggeffects) library(effects) library(ordinal) library(modelsummary) names(MY_CGSS)
sat_mod1 <- lm(Pre_Sex ~ fair+edu, # regression formula data = MY_CGSS) # data sat_mod2<-lm(Pre_Sex ~ fair+edu + gender, data = MY_CGSS) sat_mod3<-lm(Pre_Sex ~ fair+edu + gender+ageedugender, data = MY_CGSS) # model marginsplot ggeffects mydf<-ggpredict(sat_mod3, terms = c(“gender”, “edu”),data = MY_CGSS) ggplot(mydf, aes(x = x, y = predicted, colour = group)) + geom_point() + stat_smooth(method = “lm”, se = FALSE)
mydf1<-ggpredict(sat_mod3, terms = c(“edu”, “age”),data = MY_CGSS) ggplot(mydf1, aes(x = x, y = predicted, colour = group)) + geom_point() + stat_smooth(method = “lm”, se = FALSE)
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)