You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用bnlearn处理.sav文件时遇离散变量错误及网络差异问题求助

Troubleshooting Your bnlearn & SPSS .sav File Issues

Hey there! No need to apologize at all—we all start somewhere with Bayesian networks and bnlearn. Let's break down your two main issues step by step:

1. Why do network structures differ between .sav and CSV exports?

This is actually pretty common, and it boils down to how SPSS handles variable types, encoding, and missing values when converting .sav to CSV:

  • Variable type discrepancies: .sav files store metadata about categorical variables (like labels and factor levels) that CSV files don’t preserve. When you export to CSV, SPSS might convert categorical variables to raw numeric values or strings, which bnlearn interprets differently than the labelled types from read_sav. The hc algorithm uses scoring metrics (like BIC) that calculate differently for discrete (factor) vs. continuous/numeric variables, leading to different learned structures.
  • Missing value handling: .sav files have dedicated missing value codes, which CSV might convert to empty strings or generic NA values. bnlearn handles missing values in structure learning differently depending on how they’re encoded, which can also shift the resulting network.

2. Fixing the "only discrete Bayesian networks can be exported into DSC format" error

The root issue here is that read_sav imports categorical variables from SPSS as labelled vectors (from the haven package), not as R’s native factor type. Even though you recoded them in SPSS, bnlearn’s write.net requires explicit discrete (factor) variables to export to DSC format (used by SamIam).

Here’s how to fix it:

Step 1: Convert labelled variables to factors

Use haven’s as_factor() function to convert all labelled columns in your dataset to factors. You can do this with dplyr for a tidy approach:

require(bnlearn)
library(haven)
library(dplyr)

# Read the .sav file
path <- file.path("C:/.../path_to_file/...", "database.sav")
rehosp.data <- read_sav(path)

# Convert all labelled variables to factors
rehosp.data <- rehosp.data %>% 
  mutate(across(where(is.labelled), as_factor))

Or with base R if you don’t use dplyr:

rehosp.data[] <- lapply(rehosp.data, function(x) {
  if (is.labelled(x)) as_factor(x) else x
})

Step 2: Verify variable types

Run str(rehosp.data) to confirm all your categorical variables are now listed as Factor (not dbl+labelled or other types):

print(str(rehosp.data))

Step 3: Re-run your bnlearn workflow

Once variables are properly converted to factors, re-run your structure learning and fitting code:

rehosp.hc.net <- hc(rehosp.data, debug=TRUE)
plot(rehosp.hc.net)

rehosp.hc.fit <- bn.fit(rehosp.hc.net, rehosp.data)
write.net(rehosp.hc.fit, file="rehosp.hc.learned.net")

Bonus: Aligning .sav and CSV results

To make your .sav and CSV-based network structures match, ensure:

  • Both datasets have variables converted to the same type (factors for categorical variables)
  • Missing values are handled consistently (e.g., use na.omit() on both or let bnlearn handle them the same way)

内容的提问来源于stack exchange,提问作者msap

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 06:39:16