简体   繁体   English

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

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

I desperately need help!我迫切需要帮助!

I am trying to predict drug use based on 5 characteristics: Age, Gender, Education, Ethnicity, Country.我试图根据 5 个特征来预测吸毒情况:年龄、性别、教育、种族、国家。 I already build a tree model in R with rpart我已经用 rpart 在 R 中构建了一棵树 model

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

, a logistic regression model , 逻辑回归 model

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

, and a knn model , 和一个 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)) . KnnModel <- train(form = DrugUser~., data = ModelData,method ='knn',tuneGrid=expand.grid(.k=1:100),metric='Accuracy',trControl=trainControl(method='repeatedcv',number=10,repeats=10))

I saved those as RDS files and uploaded them successfully in Power BI.我将它们保存为 RDS 文件并在 Power BI 中成功上传。

I then created tables for each characterization and created okviz filters for them.然后,我为每个特征创建了表格,并为它们创建了 okviz 过滤器。

Then I tried to predict whether a customer gets predicted as a drug user or a non-drug user based on the selections in the okviz filters.然后,我尝试根据 okviz 过滤器中的选择来预测客户是否被预测为吸毒者或非吸毒者。 This is when everything went horribly wrong:这是一切都变得非常错误的时候:

I created a custom R visual vor each model prediction and inserted the following code in each visual:我为每个 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)

Error: Can't determine relationship between fields.错误:无法确定字段之间的关系。

What has gone wrong here?这里出了什么问题?

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. 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.

I need help as I am literally going crazy over this and I don't know how to resolve the issue!我需要帮助,因为我真的为此发疯了,我不知道如何解决这个问题! I would be really happy if you can help me如果你能帮助我,我会很高兴

enter image description here在此处输入图像描述

You made a error in line 34, and line 25. Below is a fixed version of your code.您在第 34 行和第 25 行出错。下面是您的代码的固定版本。 # The following code to create a dataframe and remove duplicated rows is always executed and acts as a preamble for your script: # 以下用于创建 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 = 

Good Luck!祝你好运!

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

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