Johannes Radinger
2013-May-08 08:59 UTC
[R] Fuzzy rules definition (package "sets") from data.frame
Hi,
I just discovered the package "sets" and its ability to perform fuzzy
systems calculations.
The example in the manual of fuzzyinference() gives an overview how to
develop rules.
However my "rules" are already available as data.frame and I'd
like to
convert them into the format
that is needed by fuzzy_rules.
Here how the rules would be defined manually:
rules <-
set(
fuzzy_rule(depth %is% low && velocity %is% medium, suitability %is%
low),
fuzzy_rule(depth %is% medium && velocity %is% medium, suitability
%is%
medium),
fuzzy_rule(depth %is% high && velocity %is% low, suitability %is%
low)
)
In my case I have the rules in following dataframe where a combination of
depth and velocity (&&)
result in a certain suitability. So every single row is actually a rule:
df <-
data.frame(depth=c("low","medium","high"),velocity=c("medium","medium","low"),suitability=c("low","medium","low"))
Is there an easy way to combine the columns per row in a way that in the
end I get the same format
as the object "rules"? As a single rule (fuzzy_rule) is not a pure
text
string I don't know how to do that resp. to combine
all rules/rows into a set().
Any suggestions?
Best regards,
Johannes
[[alternative HTML version deleted]]
Johannes Radinger
2013-May-08 10:52 UTC
[R] Fuzzy rules definition (package "sets") from data.frame
Following my last mail, I found a simple solution using eval(parse)):
df <-
data.frame(depth=c("low","medium","high"),velocity=c("medium","medium","low"),suitability=c("low","medium","low"))
df$rule <- paste("fuzzy_rule(depth %is%
",df[,"depth"]," && ","velocity
%is% ",df[,"velocity"],", ","suitability %is%
",df[,"suitability"],")",sep="")
rules <-
eval(parse(text=paste("set(",paste(df[,"rule"],collapse=","),")",sep="")))
which turns the dataframe into the rules definition object similar to:
rules <-
set(
fuzzy_rule(depth %is% low && velocity %is% medium, suitability %is%
low),
fuzzy_rule(depth %is% medium && velocity %is% medium, suitability
%is%
medium),
fuzzy_rule(depth %is% high && velocity %is% low, suitability %is%
low)
)
/johannes
[[alternative HTML version deleted]]