gpt4 book ai didi

R - 总结相对学期序列的类(class)注册

转载 作者:行者123 更新时间:2023-12-04 11:38:02 26 4
gpt4 key购买 nike

应用问题

我想抽象出代码,这些代码总结了一组学生的 n 门类(class)和 n 学期的类(class)学习模式和成功率。

例子

对于以下一组学生,有多少人在学习了“A”类(class)后进入了“B”类(class),其中有多少学生成功了:

data <- data.frame(student = c(1, 1, 1, 2, 2, 2, 3, 4, 4, 5, 5, 5),
term = c(2, 3, 3, 1, 2, 3, 2, 1, 3, 1, 2, 4),
course = c('A', 'B', 'C', 'A', 'B', 'C', 'A', 'B', 'C', 'A', 'A', 'C'),
success = c(1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1),
stringsAsFactors = FALSE)

我们可以用下面的代码回答这个问题:
library(dplyr) 

# Get each student's first, second, third, ... term.
data <- data %>%
group_by(student) %>%
mutate(term_dense = dense_rank(term))%>%
ungroup()

# Identify those who took course A
courseA <- data %>%
filter(course == "A")%>%
select(student, courseA_dense = term_dense)

# Get records of students who took course A, and their subsequent courses
data <- data %>%
left_join(courseA, by = "student")%>%
filter(term_dense >= courseA_dense) # >= for courses they took in same term as course "A"

# Summarise for each term_dense
data %>%
group_by(term_dense) %>%
summarise(attempted_course_A = sum(course == "A"),
completed_course_A = sum(course == "A" & success == 1),
attempted_course_B = sum(course == "B"),
completed_course_B = sum(course == "B" & success == 1))

产生:
# A tibble: 3 x 5
term_dense attempted_course_A completed_course_A attempted_course_B completed_course_B
<int> <int> <int> <int> <int>
1 1 4 2 0 0
2 2 2 2 2 2
3 3 0 0 0 0

我们可以看到尝试过类(class) A 的学生,两个尝试过类(class) B 的学生都成功了。

现在,我可以通过在 summarise 中添加行来计算参加类(class)“A”后参加类(class)“C”的人数。声明(即 completed_course_C = sum(course == "C" & success == 1) ),但如果我有很多类(class),它似乎不是最有效的选择。

此外,如果我想在类(class)“Y”之后总结类(class)“X”的顺序,对于任何“X”和“Y”,它会创建更多 summarise 的排列。陈述。我如何看待那些在“Z”之后拿“X”在“Y”之后的人。

所以, 我如何总结可变数量的类(class)在可变数量的期限内的类(class)进度和成功率?

期望的输出

我想这就是我的一些困难所在。我不知道结果是什么 data.frame需要像这样的结构。

不过,我确实知道我想轻松回答以下一般性问题:

“X% 的学生在“A”类(class)中取得成功,随后又参加了“B”类(class),成功率为 Y%”

抽象问题

我一直在尝试将一般问题(队列跟踪/排序?)应用于其他领域,以便在 google 和 Stack Overflow 中获得更好的关键词/搜索结果。一种似乎很有希望的方法是使用网络分析。

具体来说, this post, Network Analysis with R ,有助于确定潜在的解决方案。我跟着这篇文章,改用我的数据,并且能够得到我大约一半的信息。使用这种方法,我只能获得一系列尝试或一系列成功率——不能同时获得两者。但我才刚刚开始学习网络分析。

实际上,我已经能够使用 plotly 's sankey diagram 手动可视化摘要。 ,它使用类似的网络/链接框架。但我仍然无法以编程方式计算该信息。

其他尝试

鉴于我想基本上将摘要函数“映射”到我的数据,我的许多尝试都使用了 purrr带有嵌套列表列的包。
purrr尝试

使用原 data从上面,我试图按照他们的排名条款嵌套一个学生的类(class)列表。
# library(dplyr) # Loaded in above example
library(tidyr)
library(purrr)

data <- data %>%
group_by(student) %>%
mutate(term_dense = dense_rank(term)) %>%
ungroup()%>%
nest(term, course, success, .key = "schedule")

然后我尝试创建一个函数,它将源类(class)的摘要返回到目标类(class),最终目标是 map将此函数添加到包含源和目标的所有唯一排列的列表:
attempt_summary <- function(df, source, target){

temp_df <- df %>%
filter(map_lgl(schedule, ~any(.x$course == source)))%>%
select(student, source_term_dense = term_dense)

df <- df %>%
left_join(temp_df, by = "student")%>%
filter(term_dense >= source_term_dense)

df %>%
group_by(term_dense) %>%
summarise(completed_source = sum(map_int(schedule, ~any(.x$course == source & .x$success == 1))),
attempted_target = sum(map_int(schedule, ~any(.x$course == target))),
completed_target = sum(map_int(schedule, ~any(.x$course == target & .x$success == 1))))

}

该功能适用​​于一个示例,
attempt_summary(data, "A", "B")

# A tibble: 3 x 4
term_dense completed_source attempted_target completed_target
<int> <int> <int> <int>
1 1 2 0 0
2 2 2 2 2
3 3 0 0 0

但我不知道如何将它映射到其他所有内容(我什至不知道如何构建我的目标和源列表)但这是我的尝试:
# DO NOT RUN - DOESN'T WORK
# map(data, attempt_summary, source = src_list, target = trgt_list)

堆栈溢出帖子

除了许多其他关于 purrr ,我在寻找解决方案时引用了这些帖子,但都不是我想要的。
  • Tracking cohort over time in R
  • Sequence Analysis of Consumer Baskets

  • session 信息

    这是我的 sessionInfo() 的输出称呼:
    > sessionInfo()
    R version 3.5.3 (2019-03-11)
    Platform: x86_64-w64-mingw32/x64 (64-bit)
    Running under: Windows 10 x64 (build 17763)

    Matrix products: default

    locale:
    [1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252
    [3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
    [5] LC_TIME=English_United States.1252

    attached base packages:
    [1] stats graphics grDevices utils datasets methods base

    other attached packages:
    [1] purrr_0.3.2 tidyr_0.8.3 dplyr_0.8.0.1

    loaded via a namespace (and not attached):
    [1] Rcpp_1.0.1 fansi_0.4.0 utf8_1.1.4 crayon_1.3.4 assertthat_0.2.1 R6_2.4.0
    [7] magrittr_1.5 pillar_1.3.1 cli_1.1.0 rlang_0.3.4 rstudioapi_0.10 tools_3.5.3
    [13] glue_1.3.1 compiler_3.5.3 pkgconfig_2.0.2 tidyselect_0.2.5 tibble_2.1.1

    最佳答案

    这是你中间问题的一部分,关于“X% 的学生在“A”类(class)中取得成功,随后参加了“B”类(class)并且成功率为 Y%”。

    这会发现每门类(class) A 成功和每门类(class) A 不成功的 Y%。

    library(tidyverse)
    data2 <- data %>%
    left_join(data, by = c("student")) %>% # add future course results to each result that has any
    filter(term.y > term.x) %>% # includes all future courses; could limit to just next one?
    count(course.x, success.x, course.y, success.y) %>%
    spread(success.y, n, fill = 0) %>%
    mutate(success_rate = `1`/ (`0` + `1`)) %>%
    select(course.x:course.y, success_rate) %>%
    spread(course.y, success_rate)

    结果:每个“事件 1”为一行,每列中 future 类 Y 的成功率。这表明参加 A 的人通过了所有后续类(class),无论他们在 A 中的表现如何。参加 B 的人在 C 上的通过率为 50-50。
    > data2
    # A tibble: 3 x 5
    course.x success.x A B C
    <chr> <dbl> <dbl> <dbl> <dbl>
    1 A 0 1 NA 1
    2 A 1 NA 1 1
    3 B 1 NA NA 0.5

    关于R - 总结相对学期序列的类(class)注册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55856374/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com