如何将列联表(计数)转换为 R 中 GLM 的个体
How convert contingency tables (counts) to individuals for GLM in R
我有这张照片中的信息:
您可以在这里下载:https://drive.google.com/file/d/1pgO51NXtjpVSz-VxQEDNFFuQXVc4jVkt/view?usp=sharing
我想要的是将这些数据转化为个人,
例如
会变成这样
另一个例子
会变成这样
所以,如果我们说 n="原始 data.frame 中所有数字的总和",即所有个体的数量,最终输出将是一个 data.frame
有 6 列和 n 行。
我想在 R 中执行此操作,但不知道如何操作。一旦我有了这个,我想做的就是应用一个具有家庭二项式和 link = probit.
的广义线性模型
现在,这个页面可以解释我尝试做的一些事情:
https://www.datanalytics.com/libro_r/la-funcion-melt-y-datos-en-formato-largo.html
好的......我有一个答案,但是......我想知道是否存在任何概括。开始了:
library(readxl)
library(dplyr)
# Información original ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
byssinosis <- read_xls(path = "byssinosis.xls",range = "B4:K27",col_names = F)
names(byssinosis) <- c("Employment","Smoking","Sex","Race",
"W1y","W1n","W2y","W2n","W3y","W3n")
# View(byssinosis)
# Procesando la información a individuos ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Primero pasamos las columnas a una sola.
datos <- reshape2::melt(byssinosis)
# Separamos estas columnas en las dos características deseadas.
datos <- datos %>%
mutate(Workplace = ifelse(variable %in% c("W1y", "W1n"),1,
ifelse(variable %in% c("W2y", "W2n"),2,3)),
Byssinosis = ifelse(variable %in% c("W1y", "W2y", "W3y"),"yes","no"))
# Repetimos con base en value.
individuos=rep(seq_len(nrow(datos)),datos$value)
datos <- datos[individuos,]
# Nos quedamos solo las columnas deseadas
datos <- datos %>% select(-c(variable,value))
# View(datos)
# Comprobación ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tabla <-
table(datos) %>%
as.data.frame() %>%
arrange(Employment, desc(Smoking), desc(Sex), desc(Race), Workplace, desc(Byssinosis))
# View(tabla)
试试这个
library(readxl)
library(dplyr)
library(tidyr)
df <- read_xls("byssinosis.xls", range = cell_rows(c(4L, NA_integer_)), col_names = FALSE)
raw_nms <- read_xls("byssinosis.xls", range = cell_rows(c(1L, 3L)), col_names = FALSE)
names(df) <- with(
fill(as.data.frame(t(raw_nms)[, -2L]), V1, V2), # replace any missing value in V1 and V2 (i.e. row 1 and 3 in your excel) with the last observation carrired forward
trimws(paste(V1, if_else(is.na(V2), "", V2))) # collapse these names into a single vector
)
df %>%
pivot_longer(contains(" "), names_to = c("Workplace", "byssinosis"), names_pattern = "(\d+) (.+)") %>%
slice(inverse.rle(list(lengths = value, values = seq_along(value)))) %>%
select(-value)
输出
# A tibble: 5,419 x 6
Employment Smoking Sex Race Workplace byssinosis
<chr> <chr> <chr> <chr> <chr> <chr>
1 <10 yes M W 1 yes
2 <10 yes M W 1 yes
3 <10 yes M W 1 yes
4 <10 yes M W 1 no
5 <10 yes M W 1 no
6 <10 yes M W 1 no
7 <10 yes M W 1 no
8 <10 yes M W 1 no
9 <10 yes M W 1 no
10 <10 yes M W 1 no
# ... with 5,409 more rows
我有这张照片中的信息:
您可以在这里下载:https://drive.google.com/file/d/1pgO51NXtjpVSz-VxQEDNFFuQXVc4jVkt/view?usp=sharing
我想要的是将这些数据转化为个人,
例如
会变成这样
另一个例子
会变成这样
所以,如果我们说 n="原始 data.frame 中所有数字的总和",即所有个体的数量,最终输出将是一个 data.frame
有 6 列和 n 行。
我想在 R 中执行此操作,但不知道如何操作。一旦我有了这个,我想做的就是应用一个具有家庭二项式和 link = probit.
的广义线性模型现在,这个页面可以解释我尝试做的一些事情:
https://www.datanalytics.com/libro_r/la-funcion-melt-y-datos-en-formato-largo.html
好的......我有一个答案,但是......我想知道是否存在任何概括。开始了:
library(readxl)
library(dplyr)
# Información original ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
byssinosis <- read_xls(path = "byssinosis.xls",range = "B4:K27",col_names = F)
names(byssinosis) <- c("Employment","Smoking","Sex","Race",
"W1y","W1n","W2y","W2n","W3y","W3n")
# View(byssinosis)
# Procesando la información a individuos ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Primero pasamos las columnas a una sola.
datos <- reshape2::melt(byssinosis)
# Separamos estas columnas en las dos características deseadas.
datos <- datos %>%
mutate(Workplace = ifelse(variable %in% c("W1y", "W1n"),1,
ifelse(variable %in% c("W2y", "W2n"),2,3)),
Byssinosis = ifelse(variable %in% c("W1y", "W2y", "W3y"),"yes","no"))
# Repetimos con base en value.
individuos=rep(seq_len(nrow(datos)),datos$value)
datos <- datos[individuos,]
# Nos quedamos solo las columnas deseadas
datos <- datos %>% select(-c(variable,value))
# View(datos)
# Comprobación ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tabla <-
table(datos) %>%
as.data.frame() %>%
arrange(Employment, desc(Smoking), desc(Sex), desc(Race), Workplace, desc(Byssinosis))
# View(tabla)
试试这个
library(readxl)
library(dplyr)
library(tidyr)
df <- read_xls("byssinosis.xls", range = cell_rows(c(4L, NA_integer_)), col_names = FALSE)
raw_nms <- read_xls("byssinosis.xls", range = cell_rows(c(1L, 3L)), col_names = FALSE)
names(df) <- with(
fill(as.data.frame(t(raw_nms)[, -2L]), V1, V2), # replace any missing value in V1 and V2 (i.e. row 1 and 3 in your excel) with the last observation carrired forward
trimws(paste(V1, if_else(is.na(V2), "", V2))) # collapse these names into a single vector
)
df %>%
pivot_longer(contains(" "), names_to = c("Workplace", "byssinosis"), names_pattern = "(\d+) (.+)") %>%
slice(inverse.rle(list(lengths = value, values = seq_along(value)))) %>%
select(-value)
输出
# A tibble: 5,419 x 6
Employment Smoking Sex Race Workplace byssinosis
<chr> <chr> <chr> <chr> <chr> <chr>
1 <10 yes M W 1 yes
2 <10 yes M W 1 yes
3 <10 yes M W 1 yes
4 <10 yes M W 1 no
5 <10 yes M W 1 no
6 <10 yes M W 1 no
7 <10 yes M W 1 no
8 <10 yes M W 1 no
9 <10 yes M W 1 no
10 <10 yes M W 1 no
# ... with 5,409 more rows