繁体   English   中英

Power BI - 创建自定义 r 视觉对象关系错误

[英]Power BI - creating custom r visuals relationship error

我迫切需要帮助!

我试图根据 5 个特征来预测吸毒情况:年龄、性别、教育、种族、国家。 我已经用 rpart 在 R 中构建了一棵树 model

DrugTree3 <- rpart(formula = DrugUser ~ Age+Gender+Education+Ethnicity+Country, data = traindata)

, 逻辑回归 model

DrugLog <- glm(formula = DrugUser ~ Age+Gender+Ethnicity+Education+Country,data = traindata, family = binomial)

, 和一个 knn model

KnnModel <- train(form = DrugUser~., data = ModelData,method ='knn',tuneGrid=expand.grid(.k=1:100),metric='Accuracy',trControl=trainControl(method='repeatedcv',number=10,repeats=10))

我将它们保存为 RDS 文件并在 Power BI 中成功上传。

然后,我为每个特征创建了表格,并为它们创建了 okviz 过滤器。

然后,我尝试根据 okviz 过滤器中的选择来预测客户是否被预测为吸毒者或非吸毒者。 这是一切都变得非常错误的时候:

我为每个 model 预测创建了一个自定义 R 视觉对象,并在每个视觉对象中插入了以下代码:

# The following code to create a dataframe and remove duplicated rows is always executed and acts as a preamble for your script: 

# dataset <- data.frame(chunk_id, model_id, model_str, AgeLabel, GenderLabel, CountryLabel, EducationLabel, EthnicityLabel)
# dataset <- unique(dataset)

# Paste or type your script code here:

library(dplyr)
from_byte_string = function(x) {
 xcharvec = strsplit(x, " ")[[1]]
 xhex = as.hexmode(xcharvec)
 xraw = as.raw(xhex)
 unserialize(xraw)
}
# R Visual imports tables with read.csv but no argument for strings_as_factors = F.
# This means some of the chunks are truncated (ie if they had a " " at the end).
# If you convert to a character and add a space if nchar == 9999 the deserialization works.
# (Thanks to Danny Shah)
dataset <- dataset %>%
 mutate( model_str = as.character(model_str) ) %>%
 mutate( model_str = ifelse(nchar(model_str) == 9999, paste0(model_str, " "), model_str) )
model_vct <- dataset %>%
 filter(model_id == 1) %>%
 distinct(model_id, chunk_id, model_str) %>%
 arrange(model_id, chunk_id) %>%
 pull(model_str)
finalfit.str <- paste( model_vct, collapse = "" )
finalfit <- from_byte_string(finalfit.str)
# get the user parameters
userdata <- dataset %>% select(AgeLabel,GenderLabel,CountryLabel,EducationLabel,EthnicityLabel) %>% unique()
# and then using them to make a prediction
myprediction <- predict(finalfit,newdata=data.frame(Age=userdata$AgeLabel,Gender=userdata$GenderLabel,Country=userdata$CountryLabel, Education=userdata$EducationLabel,Ethnicity=userdata$EthnicityLabel))
maxpred <- which(myprediction==max(myprediction))
myclass <- maxpred - 1
myprob <- myprediction[[maxpred]]
plot.new()
text(0.5,0.5,labels=sprintf("P(class = %s) = %s",myclass,as.character(round(myprob,2))),cex=3.5)

错误:无法确定字段之间的关系。

这里出了什么问题?

When I then clicked on the diagonal arrow to get to R Studio, this happens: Unable to construct R script data for use in external R IDE.

我需要帮助,因为我真的为此发疯了,我不知道如何解决这个问题! 如果你能帮助我,我会很高兴

在此处输入图像描述

您在第 34 行和第 25 行出错。下面是您的代码的固定版本。 # 以下用于创建 dataframe 并删除重复行的代码始终被执行并充当脚本的前导:

# dataset <- data.frame(chunk_id, model_id, model_str, AgeLabel, GenderLabel, CountryLabel, EducationLabel, EthnicityLabel)
# dataset <- unique(dataset)

# Paste or type your script code here:

library(dplyr)
from_byte_string = function(x) {
 xcharvec = strsplit(x, " ")[[1]]
 xhex = as.hexmode(xcharvec)
 xraw = as.raw(xhex)
 unserialize(xraw)
}
# R Visual imports tables with read.csv but no argument for strings_as_factors = F.
# This means some of the chunks are truncated (ie if they had a " " at the end).
# If you convert to a character and add a space if nchar == 9999 the deserialization works.
# (Thanks to Danny Shah)
dataset <- dataset %>%
 mutate( model_str = as.character(model_str) ) %>%
 mutate( model_str = ifelse(nchar(model_str) == 9999, paste0(model_str, " "), model_str) )
model_vct <- dataset %>%
 filter(model_id == 1) %>%
 distinct(model_id, chunk_id, model_str) %>%
 arrange(model_id, chunk_id) %>%
 pull(model_str)
finalfit.str <- paste( model_vct, collapse = "" )
finalfit <- from_byte_string(finalfit.str)
# get the user parameters
userdata <- dataset %>% select(AgeLabel,GenderLabel,CountryLabel,EducationLabel,EthnicityLabel) %>% unique()
# and then using them to make a prediction
myprediction <- predict(finalfit,newdata=data.frame(Age=userdata$AgeLabel,Gender=userdata$GenderLabel,Country=userdata$CountryLabel, Education=userdata$EducationLabel,Ethnicity=userdata$EthnicityLabel))
maxpred <- which(myprediction==max(myprediction))
myclass <- maxpred - 1
myprob <- myprediction[[maxpred]]
plot.new()
text(0.5,0.5,labels=sprintf("P(class = 

祝你好运!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM