Related
users,
I have received a data from a conjoint survey experiment. What I want to do is to reshape from wide to long format. However, this seems to be slightly complicated. I am pretty sure it is possible to do with cj_tidy (package cregg) but can't solve it myself.
In the survey, the respondents were asked to compare two organizations that vary across 7 profiles (Efficiency Opennes Inclusion Leader Gain & System). In total, respondents were presented with four comparisons. So 2 organizations and 4 comparisons (4x2). They had to choose one of the presented organization and rate them separately after choosing one.
At the moment, the profile variables are structured in this way: org1_Efficiency_conj_1, org1_Opennes_conj1 ..etc. The first part "org" indicates whether it is the first or second organization. The last part "conj", indicated the order of the conjoint/comparison, where the "conj4" is the last comparison. The CHOICE variables also follow the order of conjoint – for example,"CHOICE_conj1", "CHOICE_conj2", where =1 means the respondent chose "org1". If =2, then org2 was chosen. The RATING> variable indicates a value from 0 to 10 for each organization: RATING_conj1_org1; RATING_conj1_org2 etc..
The current wide format of the data is not suitable for conjoint analysis - what I need is to create 8 observations for each respondent (4x2=8) where the variable CHOICE would indicate which of the organizations were chosen (where =1 if yes; and =0 if no). In a similar way, the variable RATING should indicate the rating given by respondents for both of the organizations (0 to 10).
This is how I would like the data to look like:
Note please that there are also covariates such as Q1 and Q2 in the picture, they are not a part of the experiment and should remain constant for each individual observation.
Below I share 50 observations from my real data.
> dput(cjdata_wide) structure(list(ID = 1:50, org1_Effeciency_conj_1 =
> c(3L, 2L, 1L, 3L, 3L, 2L, 3L, 3L, 3L, 3L, 2L, 1L, 1L, 1L, 1L, 1L, 3L,
> 2L, 3L, 3L, 3L, 2L, 3L, 1L, 2L, 1L, 3L, 3L, 1L, 1L, 3L, 1L, 1L, 3L,
> 3L, 2L, 3L, 2L, 3L, 2L, 1L, 1L, 3L, 2L, 1L, 1L, 1L, 2L, 2L, 1L ),
> org1_Oppenes_conj_1 = c(3L, 3L, 1L, 3L, 1L, 3L, 2L, 3L, 2L, 3L, 1L,
> 1L, 1L, 2L, 3L, 2L, 2L, 1L, 3L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 1L, 3L,
> 1L, 2L, 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L, 5L, 5L, 5L, 1L, 5L, 5L, 1L, 1L, 1L, 1L, 1L,
> 1L, 1L, 1L, 1L, 1L, 1L, 1L ), educ = c(6L, 5L, 2L, 5L, 6L, 6L, 4L, 6L,
> 3L, 5L, 4L, 5L, 6L, 4L, 4L, 6L, 6L, 6L, 3L, 6L, 5L, 6L, 5L, 5L, 3L,
> 4L, 6L, 6L, 5L, 3L, 3L, 4L, 3L, 6L, 3L, 5L, 5L, 6L, 3L, 5L, 3L, 3L,
> 3L, 3L, 4L, 5L, 5L, 4L, 2L, 3L)), class = "data.frame", row.names =
> c(NA,
> -50L))
What I have done so far is this:
library(cregg)
str(long <- cj_tidy(cjdata_wide,
profile_variables = c("All the profile variables"),
task_variables = c("CHOICE AND RATING VARIABLES HERE"),
id = ~ id))
stopifnot(nrow(long) == nrow(data)*4*2
But I'm keep getting errors. I have tried to follow the example given by the cregg package - but with no success. Any help is much appreciated! I am open to all possible ways, be it so through cregg package or tidyr for instance.
Your data not being in a standard form mades this a difficult problem. Here is a solution using the tidyr package.
The solutions involves 3 parts, dealing with the profiles, the rating and finally the rating choice.
The key to the profiles part was to pivot long and breaking up the profile names into component parts and then pivot wider for the column headings.
The rating and binary choice involved pivoting longer and then aligning the rows.
library(tidyr)
library(dplyr)
#Get the categories part correct
answer <-cjdata_wide %>% pivot_longer(cols=starts_with("org"), names_to=c("org", "Cat", "conj", "order"), values_to= "values", names_sep="_") %>% select(-c("conj"))
answer <-answer %>% select(!starts_with("RATING") & !starts_with("CHOICE"))
answer <-pivot_wider(answer, names_from = "Cat", values_from = "values")
#get the ratings column corretn
rating <-cjdata_wide %>% select(starts_with("RATING") )
rating <- rating%>% pivot_longer(cols=everything(), names_to=c("Rating", "conj", "order"), values_to= "Choice_Rating", names_sep="_") %>% select(-c("conj"))
answer$Choice_Rating <- rating$Choice_Rating
#Get the choice correct
choice <-cjdata_wide %>% select(starts_with("CHOICE") )
choiceRate <- choice%>% pivot_longer(cols=everything(), names_to=c("Choice", "conj"), values_to= "Choice_Rating", names_sep="_") %>% select(-c("conj"))
answer$Choice_binary <-ifelse(substr(answer$org, 4,4) == rep(choiceRate$Choice_Rating,each=2), 1, 0)
answer
It may be possible to simplify the above. Good luck.
Update per Comment
The final data frame has pairs of rows which corresponds to org 1 or 2. I duplicated the choice so that Choice_Rating column is the same length as the Organization ("org" column). I then compared Choice_Rating & Organization and setting the final value to either 0 or 1 depending on the match.
For question in the comment, A simple way is to convert the factor column to integers with as.integer() function, then the first factor becomes 1 and the second becomes 2 etc. (may need to relevel in order to get the proper order).
Another option is to create a new "org" column with your factor names properly listed.
Hopefully this provides enough guidance.
I have a time series data at hourly level. I am trying to build a forecast for that data. The following is sample of data:
sample <-
structure(list(group_type = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("Group 1",
"Group 2", "Group 5"), class = "factor"), sub_group_type = structure(c(1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L), .Label = c("Sub Group 1", "Sub Group 2", "Sub Group 3"),
class = "factor"), date = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("1/1/17",
"1/2/17", "1/3/17"), class = "factor"), hour = c(6L, 7L, 8L, 9L, 10L, 11L, 12L,
6L, 7L, 8L, 9L, 10L, 11L, 12L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 6L, 7L, 8L, 9L,
10L, 11L, 12L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 6L,
7L, 8L, 9L, 10L, 11L, 12L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 6L, 7L, 8L, 9L, 10L,
11L, 12L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 6L, 7L,
8L, 9L, 10L, 11L, 12L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 6L, 7L, 8L, 9L, 10L, 11L,
12L, 6L, 7L, 8L, 9L, 10L, 11L, 12L), weekday = structure(c(2L, 2L, 2L, 2L, 2L,
2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L),
.Label = c("Monday", "Sunday", "Tuesday"), class = "factor"), total = c(9L, 9L,
10L, 6L, 2L, 14L, 3L, 11L, 12L, 12L, 0L, 10L, 8L, 13L, 14L, 17L, 12L, 5L, 9L, 7L,
10L, 13L, 23L, 11L, 3L, 6L, 10L, 11L, 14L, 16L, 13L, 2L, 3L, 4L, 14L, 11L, 16L,
8L, 12L, 7L, 6L, 13L, 13L, 22L, 12L, 7L, 9L, 8L, 14L, 9L, 16L, 15L, 6L, 7L, 6L,
12L, 13L, 14L, 7L, 3L, 13L, 11L, 6L, 8L, 15L, 11L, 3L, 10L, 9L, 7L, 12L, 10L, 10L,
3L, 14L, 8L, 12L, 10L, 20L, 5L, 4L, 8L, 12L, 3L, 0L, 4L, 5L, 1L, 6L, 7L, 0L, 3L,
1L, 1L, 0L, 2L, 2L, 0L, 2L, 0L, 3L, 7L, 6L, 2L, 1L)), .Names = c("group_type",
"sub_group_type", "date", "hour", "weekday", "total"), class = "data.frame",
row.names = c(NA, -105L))
I am applying the following functions to the above data:
models <- function(x){
x <- msts(x, seasonal.periods=c(24,168))
mod_exp <- ets(x, ic='aicc', restrict=T)
mod_hwa <- HoltWinters(x,seasonal = "additive")
mod_hwm <- HoltWinters(x,seasonal = "multiplicative")
mod_neural <- nnetar(x, p=7, size=25)
mod_tbats <- tbats(x, ic='aicc', seasonal.periods=7)
mod_bats <- bats(x, ic='aicc', seasonal.periods=7)
mod_stl <- stlm(x, s.window=7, ic='aicc', robust=TRUE, method='ets')
mod_sts <- StructTS(x)
}
test <- by(sample,list(sample$group_type,sample$sub_group_type,sample$date, sample$hour
),models)
However, I am getting the following error:
Error in ets(x, ic = "aicc", restrict = T) : y should be a univariate time series
If I split the data as follows as apply ets() function, I am able to run it without any issues. But, this splitting of data is not a very feasible option for me as the number of Groups and Sub Groups are too many and each of them has a different time series pattern:
sub_sample_1 <- sample[sample$group_type == "Group 1" & sample$sub_group_type == "Sub Group 1",6]
x <- msts(sub_sample_1, seasonal.periods=24)
mod_arima <- auto.arima(x, ic='aicc', stepwise=F)
mod_exp <- ets(x, ic='aicc', restrict=T)
mod_hwa <- HoltWinters(x,seasonal = "additive")
mod_hwm <- HoltWinters(x,seasonal = "multiplicative")
mod_neural <- nnetar(x, p=24, size=10)
mod_tbats <- tbats(x, ic='aicc', seasonal.periods=24)
mod_bats <- bats(x, ic='aicc', seasonal.periods=24)
mod_stl <- stlm(x, s.window=24, ic='aicc', robust=TRUE, method='ets')
mod_sts <- StructTS(x)
Is there any work around so that I can apply the models by group of columns with out encountering any errors?
Also, not all models are working for all the groups. For the sub_sample_1 data, HoltWinters, neuralnet, bats and stl are giving me error and others are working
> mod_hwa <- HoltWinters(x,seasonal = "additive")
Error in decompose(ts(x[1L:wind], start = start(x), frequency = f), seasonal) :
time series has no or less than 2 periods
> mod_hwm <- HoltWinters(x,seasonal = "multiplicative")
Error in HoltWinters(x, seasonal = "multiplicative") :
data must be non-zero for multiplicative Holt-Winters
> mod_bats <- bats(x, ic='aicc', seasonal.periods=24)
Error in optim(par = param.vector$vect, fn = calcLikelihoodNOTransformed, :
function cannot be evaluated at initial parameters
I can understand why these models are not working for my data. How do I exclude them when they give errors when I apply the function?
Thanks in advance for the help!
This question is similar (extension maybe) to my other question here
Several issues arise from your current setup:
Functions return the last line if no return() is specified. So your first attempt will lose all lines except for mod_sts which will be the value test is assigned for each subset of by.
In your subset code, you are actually passing the 6th column (an atomic vector) whereas you pass all columns of dataframe in your first code attempt. This may be the reason for your error where input should be per the msts docs:
A numeric vector, ts object, matrix or data frame. It is intended that
the time series data is univariate, otherwise treated the same as
ts().
Your by is receiving four groupings, group_type, sub_group_type, date, and hour unlike your second subset code of two. Unless your data is very large, these many groupings may result with few rows or no rows, and hence not enough data points for model procedures as your last code block seems to suggest.
With that said, consider the following adjustment in returning a list of named elements by first two groupings, specifying the 6th column. And because by takes a combinations of factors which in subsetting dataframe may yield no rows, below uses tryCatch to capture any errors and return empty lists to be filtered out in final line.
models <- function(x){
x <- msts(x, seasonal.periods=c(24,168))
list(
mod_exp = ets(x, ic='aicc', restrict=T),
mod_hwa = HoltWinters(x,seasonal = "additive"),
mod_hwm = HoltWinters(x,seasonal = "multiplicative"),
mod_neural = nnetar(x, p=7, size=25),
mod_tbats = tbats(x, ic='aicc', seasonal.periods=7),
mod_bats = bats(x, ic='aicc', seasonal.periods=7),
mod_stl = stlm(x, s.window=7, ic='aicc', robust=TRUE, method='ets'),
mod_sts = StructTS(x)
)
}
# TRY/CATCH TO CAPTURE ERRORS AND RETURN EMPTY LIST
test <- by(sample[,6], list(sample$group_type, sample$sub_group_type),
function(x) tryCatch({ models(x)
}, error=function(e) return(list(NA))))
# TO REMOVE NULLs AND NAs (EMPTY ITEMS)
test <- Filter(function(i) length(i) > 0, test)
I was trying to follow this tutorial using the following dataset: Mushroom Classification. I was looking for a supervised classification problem, and I think I got it.
After running the following code...
library(caret)
dataset = read.csv("mushrooms.csv")
dim(dataset)
sapply(dataset, class)
head(dataset)
levels(dataset$class)
set.seed(100)
inTrain <- createDataPartition(y=dataset$class,p=.75,list=FALSE)
str(inTrain)
training <- dataset[inTrain,]
testing <- dataset[-inTrain,]
nrow(training)
nrow(testing)
control <- trainControl(method="cv", number=10)
metric <- "Accuracy"
train.lda <- train(class ~., data=training, method="lda", trControl=control)
... I saw the dataset had 8124 rows and 22 variables —plus the classifier—.
dim(dataset)
[1] 8124 23
However when executing train I get the following error:
Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) :
contrasts can be applied only to factors with 2 or more levels
Looking around the web, and even here in Stack Overflow, the explanation I found was that my predictor has only one factor level. Like if the class variable only took one value? Nonetheless, previously in the code I check the level of that variable, and I get its level is 2, as it takes two values.
levels(dataset$class)
[1] "e" "p"
Therefore, I do not understand why I am getting the error. What's wrong with my reasoning? What am I doing wrong?
Thank you.
Sample request:
structure(list(class = structure(c(2L, 1L, 1L, 2L, 1L, 1L, 1L,
1L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 1L,
1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L,
1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("e",
"p"), class = "factor"), cap.shape = structure(c(6L, 6L, 1L,
6L, 6L, 6L, 1L, 1L, 6L, 1L, 6L, 6L, 1L, 6L, 6L, 5L, 3L, 6L, 6L,
6L, 1L, 6L, 1L, 1L, 1L, 3L, 6L, 6L, 3L, 6L, 1L, 6L, 6L, 6L, 1L,
6L, 5L, 6L, 6L, 1L, 1L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 3L, 6L,
6L, 1L, 6L, 6L, 1L, 3L, 1L, 6L, 6L, 5L, 1L, 1L, 1L, 1L, 3L, 6L,
3L, 6L, 6L, 3L, 1L, 3L, 6L, 1L, 3L, 6L, 3L, 6L, 3L, 6L, 6L, 3L,
6L, 6L, 6L, 1L, 6L, 3L, 5L, 6L, 1L, 6L, 6L, 6L, 6L, 3L, 6L, 1L,
6L), .Label = c("b", "c", "f", "k", "s", "x"), class = "factor"),
cap.surface = structure(c(3L, 3L, 3L, 4L, 3L, 4L, 3L, 4L,
4L, 3L, 4L, 4L, 3L, 4L, 1L, 1L, 1L, 3L, 4L, 3L, 3L, 4L, 4L,
4L, 3L, 3L, 4L, 4L, 1L, 3L, 3L, 4L, 4L, 4L, 4L, 1L, 1L, 4L,
1L, 3L, 4L, 4L, 1L, 4L, 3L, 4L, 4L, 3L, 4L, 4L, 4L, 3L, 3L,
4L, 3L, 4L, 1L, 3L, 3L, 4L, 1L, 4L, 3L, 4L, 4L, 3L, 3L, 4L,
4L, 1L, 1L, 4L, 1L, 4L, 3L, 3L, 3L, 4L, 4L, 4L, 3L, 4L, 1L,
1L, 4L, 3L, 3L, 3L, 4L, 1L, 1L, 3L, 4L, 4L, 3L, 3L, 4L, 3L,
3L, 4L), .Label = c("f", "g", "s", "y"), class = "factor"),
cap.color = structure(c(5L, 10L, 9L, 9L, 4L, 10L, 9L, 9L,
9L, 10L, 10L, 10L, 10L, 9L, 5L, 4L, 9L, 5L, 9L, 5L, 10L,
5L, 10L, 9L, 9L, 9L, 10L, 9L, 5L, 10L, 10L, 9L, 10L, 5L,
10L, 10L, 4L, 5L, 10L, 10L, 10L, 10L, 5L, 9L, 10L, 9L, 10L,
9L, 10L, 10L, 5L, 9L, 9L, 5L, 9L, 10L, 4L, 9L, 10L, 5L, 4L,
10L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 1L, 1L), .Label = c("a", "c",
"f", "l", "m", "n", "p", "s", "y"), class = "factor"), gill.attachment = structure(c(2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("a", "f"), class = "factor"),
gill.spacing = structure(c(1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L,
2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L,
1L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 1L,
2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L,
1L, 1L), .Label = c("c", "w"), class = "factor"), gill.size = structure(c(2L,
1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 2L,
1L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 1L,
2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 2L,
2L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("b", "n"), class = "factor"),
gill.color = structure(c(5L, 5L, 6L, 6L, 5L, 6L, 3L, 6L,
8L, 3L, 3L, 6L, 11L, 5L, 6L, 5L, 5L, 6L, 6L, 5L, 5L, 6L,
5L, 11L, 3L, 6L, 6L, 11L, 5L, 6L, 3L, 5L, 6L, 8L, 6L, 11L,
5L, 11L, 8L, 5L, 6L, 6L, 3L, 8L, 11L, 6L, 5L, 11L, 6L, 11L,
11L, 5L, 5L, 5L, 5L, 11L, 6L, 11L, 5L, 8L, 5L, 5L, 3L, 3L,
6L, 5L, 6L, 11L, 11L, 8L, 8L, 3L, 11L, 8L, 5L, 8L, 6L, 8L,
11L, 6L, 5L, 11L, 6L, 6L, 11L, 5L, 11L, 6L, 11L, 6L, 6L,
5L, 3L, 3L, 6L, 3L, 8L, 6L, 3L, 3L), .Label = c("b", "e",
"g", "h", "k", "n", "o", "p", "r", "u", "w", "y"), class = "factor"),
stalk.shape = structure(c(1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L,
2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 1L, 1L, 1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 3L, 3L, 3L), .Label = c("f", "k",
"s", "y"), class = "factor"), stalk.surface.below.ring = structure(c(3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 4L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 3L, 3L, 3L, 3L,
3L, 3L, 4L, 4L, 4L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 4L, 3L, 3L, 3L, 3L, 3L, 4L, 3L, 3L,
3L, 4L, 3L, 4L, 3L, 3L, 3L, 3L, 4L, 3L, 3L, 3L, 4L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 4L, 3L, 3L, 3L), .Label = c("f", "k",
"s", "y"), class = "factor"), stalk.color.above.ring = structure(c(8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L), .Label = c("b", "c",
"e", "g", "n", "o", "p", "w", "y"), class = "factor"), stalk.color.below.ring = structure(c(8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 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5L, 5L, 5L, 5L, 5L), .Label = c("e", "f",
"l", "n", "p"), class = "factor"), spore.print.color = structure(c(3L,
4L, 4L, 3L, 4L, 3L, 3L, 4L, 3L, 3L, 4L, 3L, 4L, 4L, 3L, 4L,
4L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 3L, 4L, 4L, 4L, 3L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 3L, 4L, 4L, 3L, 4L, 3L, 3L, 4L, 3L, 4L,
3L, 4L, 4L, 3L, 3L, 3L, 4L, 4L, 3L, 3L, 4L, 4L, 3L, 3L, 4L,
4L, 4L, 4L, 4L, 3L, 3L, 4L, 3L, 4L, 4L, 4L, 4L, 3L, 3L, 4L,
7L, 4L, 4L, 4L, 4L, 3L, 4L, 4L, 3L, 3L, 3L, 4L, 3L, 4L, 4L,
3L, 3L, 3L, 4L, 4L, 4L, 3L, 4L, 3L), .Label = c("b", "h",
"k", "n", "o", "r", "u", "w", "y"), class = "factor"), population = structure(c(4L,
3L, 3L, 4L, 1L, 3L, 3L, 4L, 5L, 4L, 3L, 4L, 4L, 5L, 1L, 6L,
1L, 4L, 4L, 4L, 4L, 5L, 4L, 3L, 4L, 5L, 3L, 3L, 6L, 5L, 3L,
4L, 3L, 6L, 4L, 5L, 5L, 4L, 5L, 4L, 4L, 6L, 6L, 5L, 3L, 3L,
4L, 3L, 4L, 4L, 4L, 4L, 3L, 5L, 5L, 4L, 1L, 3L, 3L, 6L, 5L,
4L, 4L, 3L, 4L, 1L, 4L, 4L, 3L, 5L, 5L, 4L, 5L, 4L, 4L, 5L,
5L, 6L, 5L, 6L, 4L, 4L, 6L, 4L, 4L, 4L, 4L, 4L, 6L, 5L, 6L,
4L, 4L, 3L, 1L, 4L, 4L, 3L, 4L, 4L), .Label = c("a", "c",
"n", "s", "v", "y"), class = "factor"), habitat = structure(c(6L,
2L, 4L, 6L, 2L, 2L, 4L, 4L, 2L, 4L, 2L, 4L, 2L, 6L, 2L, 6L,
2L, 2L, 6L, 6L, 4L, 2L, 4L, 4L, 4L, 2L, 4L, 4L, 6L, 1L, 4L,
6L, 4L, 5L, 4L, 1L, 6L, 6L, 1L, 4L, 2L, 5L, 6L, 2L, 4L, 2L,
4L, 4L, 5L, 5L, 2L, 2L, 4L, 6L, 6L, 4L, 2L, 2L, 2L, 5L, 6L,
4L, 4L, 4L, 2L, 2L, 2L, 2L, 2L, 6L, 1L, 4L, 1L, 5L, 2L, 1L,
1L, 5L, 6L, 2L, 2L, 2L, 6L, 2L, 2L, 2L, 2L, 2L, 2L, 6L, 6L,
2L, 2L, 4L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("d", "g",
"l", "m", "p", "u", "w"), class = "factor")), .Names = c("class",
"cap.shape", "cap.surface", "cap.color", "bruises", "odor", "gill.attachment",
"gill.spacing", "gill.size", "gill.color", "stalk.shape", "stalk.root",
"stalk.surface.above.ring", "stalk.surface.below.ring", "stalk.color.above.ring",
"stalk.color.below.ring", "veil.type", "veil.color", "ring.number",
"ring.type", "spore.print.color", "population", "habitat"), row.names = c(NA,
100L), class = "data.frame")
First five rows of data of the .csv file plus the headers
class,cap-shape,cap-surface,cap-color,bruises,odor,gill-attachment,gill-spacing,gill-size,gill-color,stalk-shape,stalk-root,stalk-surface-above-ring,stalk-surface-below-ring,stalk-color-above-ring,stalk-color-below-ring,veil-type,veil-color,ring-number,ring-type,spore-print-color,population,habitat
p,x,s,n,t,p,f,c,n,k,e,e,s,s,w,w,p,w,o,p,k,s,u
e,x,s,y,t,a,f,c,b,k,e,c,s,s,w,w,p,w,o,p,n,n,g
e,b,s,w,t,l,f,c,b,n,e,c,s,s,w,w,p,w,o,p,n,n,m
p,x,y,w,t,p,f,c,n,n,e,e,s,s,w,w,p,w,o,p,k,s,u
e,x,s,g,f,n,f,w,b,k,t,e,s,s,w,w,p,w,o,e,n,a,g
It is possible that your data set is not randomized and that what I am about to say is only true of your samples, not the full data set BUT many of your variables have only one value that is used. Type summary(dataset) and you will quickly see some examples. For example, part of the display is:
stalk.color.below.ring veil.type veil.color ring.number
w :100 p:100 n: 0 n: 0
b : 0 o: 0 o:100
c : 0 w:100 t: 0
e : 0 y: 0
g : 0
n : 0
(Other): 0
Notice that for veil.type, there is only one possible value.
levels(dataset$veil.type)
[1] "p"
I expect that is the source of your error message.
Factors = which(sapply(dataset, class) == "factor")
sapply(dataset[,Factors], function(x) { length(levels(x)) })
Shows that veil.type is the only attribute with only one possible level.
I am trying to connect sets of (two) points at each level of x, in each facet. Here is a reproducible example:
datum <- structure(list(frequency = c(8L, 7L, 6L, 18L, 5L, 11L, 16L, 15L,
9L, 8L, 8L, 10L, 2L, 20L, 14L, 3L, 6L, 2L, 2L, 11L, 10L, 6L,
15L, 19L, 18L, 18L, 8L, 2L, 10L, 15L, 12L, 17L, 1L, 18L, 7L,
8L, 16L, 4L, 9L, 2L, 7L, 3L, 16L, 7L, 18L, 20L, 9L, 10L, 13L,
2L, 15L, 7L, 3L, 20L, 4L, 15L, 5L, 7L, 9L, 16L, 5L, 8L, 10L,
10L, 7L, 10L, 10L, 17L, 7L, 8L, 13L, 13L, 16L, 5L, 20L, 18L,
13L, 19L, 3L, 8L, 14L, 12L, 20L, 2L, 9L, 13L, 7L, 2L, 5L, 5L,
13L, 9L, 13L, 7L, 9L, 4L, 4L, 20L, 1L, 4L), band = structure(c(2L,
4L, 2L, 3L, 2L, 1L, 4L, 1L, 2L, 1L, 3L, 4L, 2L, 4L, 3L, 4L, 3L,
2L, 3L, 2L, 2L, 4L, 2L, 1L, 1L, 2L, 1L, 4L, 4L, 1L, 4L, 4L, 2L,
1L, 4L, 4L, 3L, 4L, 1L, 1L, 3L, 4L, 1L, 3L, 4L, 1L, 2L, 1L, 1L,
2L, 2L, 1L, 3L, 4L, 2L, 1L, 2L, 4L, 2L, 2L, 4L, 4L, 2L, 4L, 4L,
1L, 1L, 4L, 2L, 3L, 4L, 1L, 2L, 4L, 1L, 2L, 4L, 1L, 1L, 3L, 4L,
4L, 2L, 2L, 2L, 1L, 3L, 2L, 2L, 2L, 3L, 3L, 1L, 3L, 4L, 3L, 3L,
1L, 3L, 4L), .Label = c("1", "2", "3", "4"), class = "factor"),
test = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 1L,
2L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 2L,
1L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L,
2L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 2L,
2L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 2L
), .Label = c("1", "2"), class = "factor"), knowledge = structure(c(2L,
3L, 1L, 3L, 1L, 1L, 3L, 3L, 1L, 3L, 1L, 3L, 2L, 2L, 1L, 1L,
1L, 1L, 3L, 3L, 1L, 2L, 3L, 1L, 1L, 2L, 2L, 1L, 1L, 3L, 2L,
3L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 3L, 3L, 1L, 1L, 2L, 3L,
3L, 2L, 2L, 3L, 1L, 1L, 2L, 2L, 2L, 3L, 1L, 3L, 1L, 1L, 2L,
1L, 1L, 2L, 3L, 1L, 1L, 1L, 1L, 3L, 2L, 2L, 1L, 2L, 3L, 2L,
1L, 2L, 3L, 3L, 2L, 1L, 3L, 1L, 3L, 2L, 1L, 3L, 2L, 2L, 3L,
1L, 1L, 2L, 1L, 2L, 3L, 1L, 3L, 1L), .Label = c("1", "2",
"3"), class = "factor")), .Names = c("frequency", "band",
"test", "knowledge"), row.names = c(NA, -100L), class = "data.frame")
Here is the code I have so far:
ggplot(datum, aes(knowledge, frequency, color=test)) +
stat_summary(fun.y='mean', geom='point', position=position_dodge(width=.9), size=3) +
facet_grid(~band) +
labs(y='number of words (max = 20)', x='self-report knowledge') +
scale_x_discrete(labels=c('none', 'form', 'meaning'))
Looking at the left-most facet ('1') in the graph, I would like a line to connect the pretest to posttest in the none column, another line connecting pretest to posttest in the form column, and a line connecting the pretest to the posttest in the meaning column. I would like this done in each facet.
I hope that makes sense, and thanks!
I find relying on ggplot too much for data manipulation/summarizing can hurt more than it helps. I have no idea how to connect the position-dodged points with a line. Instead, I'd do something like this:
library(dplyr)
datsum = datum %>%
group_by(band, knowledge, test) %>%
summarize(mean = mean(frequency)) %>%
ungroup %>%
mutate(knowledge_fac = factor(knowledge, labels = c('none', 'form', 'meaning')))
ggplot(datsum, aes(x = test, y = mean)) +
geom_path(aes(group = band:knowledge)) +
geom_point(aes(color = factor(test))) +
facet_grid(band ~ knowledge_fac) +
labs(y='number of words (max = 20)', x='self-report knowledge')
Borrowing from Gregor's work in munging the data, I think this does what was requested. The mutate() chunk creates Test to be a numeric offset of -0.1 for test 1 and 0.1 for test 2. This is then added to the numeric value of knowledge. The result is the numeric x passed to ggplot2. Gregor correctly defined the groups, so the rest is straightforward.
library(dplyr)
datsum <- datum %>%
group_by(band, knowledge, test) %>%
summarize(mean = mean(frequency)) %>%
mutate(Test = 0.1 * (2 * (test == 2) - 1),
Knowledge = as.numeric(knowledge) + Test) %>%
ungroup
ggplot(datsum, aes(x = Knowledge, y = mean, color = test)) +
geom_path(aes(group = band:knowledge), color = "black") +
geom_point(size = 3) +
facet_wrap(~ band, nrow = 1) +
labs(y='number of words (max = 20)', x='self-report knowledge') +
scale_color_manual(values = c("orange", "blue")) +
scale_x_continuous(limits = c(0.5, 3.5), breaks = 1:3,
labels = c("none", "form", "meaning"))
I have a data frame with x and y positions and two factor columns blocknr and cat:
dput(testData)
structure(list(xpos = c(2L, 8L, 5L, 8L, 1L, 4L, 5L, 1L, 8L, 4L,
3L, 2L, 6L, 5L, 1L, 7L, 3L, 4L, 3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 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L), .Label = c("A", "B", "C"
), class = "factor")), .Names = c("xpos", "ypos", "blocknr",
"cat"), row.names = c(NA, -77L), class = "data.frame")
I've made the following ggplot code to make 2D overview:
ggplot(data=testData, aes(x=xpos,y=ypos))+
geom_tile(aes(fill=cat), colour = "white")+
scale_fill_manual(values = c('A' = '#F8766D','C' = '#8ABF54','B' = '#C1DDA5'))+
geom_text(aes(x=xpos,y=ypos,label=blocknr),size=3)+
coord_cartesian(ylim = c(0.5, ymax + 0.5)) +
coord_cartesian(xlim = c(0.5, xmax + 0.5)) +
scale_x_continuous(breaks=seq(1,xmax,1))+
scale_y_continuous(breaks=seq(1,ymax,1))+
#geom_polygon(aes(group=blocknr))+
theme(axis.line = element_line(colour = "white"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank())
which produces the following result:
Now I would like to highlight each group of blocknrs by drawing a border around them as shown below:
I've played around with geom_polygon, geom_path, but I can't quite find a way to do this. Is there a general way to achieve this in ggplot without constructing an algorithm to compute where each line should be and add those lines as a geom_segment?
As far as I know, there is no way to do this with standard ggplot2 tile options. But it's not to much trouble to constuct them if you do it as segments. For example
ymax <- max(testData$ypos)
xmax <- max(testData$xpos)
m <- matrix(0, nrow=ymax, ncol=xmax)
m[as.matrix(testData[,2:1])] <- testData[,3]
Here we are basically taking all the row/col assignment data and creating a matrix that essentially looks like the plot but we will with the block numbers. Now, we will scan for the locations we need to add "wall" by looking for changes in the block numbers as we go across each row and column separately.
has.breaks<-function(x) ncol(x)==2 & nrow(x)>0
hw<-do.call(rbind.data.frame, Filter(has.breaks, Map(function(i,x)
cbind(y=i,x=which(diff(c(0,x,0))!=0)), 1:nrow(m), split(m, 1:nrow(m)))))
vw<-do.call(rbind.data.frame, Filter(has.breaks, Map(function(i,x)
cbind(x=i,y=which(diff(c(0,x,0))!=0)), 1:ncol(m), as.data.frame(m))))
And you can add calls to geom_segments to add the horizontal and vertical walls to the plot.
ggplot(data=testData, aes(x=xpos,y=ypos))+
geom_tile(aes(fill=cat), colour = "white")+
scale_fill_manual(values = c('A' = '#F8766D','C' = '#8ABF54','B' = '#C1DDA5'))+
geom_text(aes(x=xpos,y=ypos,label=blocknr),size=3)+
geom_segment(data=hw, aes(x=x-.5, xend=x-.5, y=y-.5, yend=y+.5))+
geom_segment(data=vw, aes(x=x-.5, xend=x+.5, y=y-.5, yend=y-.5))+
coord_cartesian(ylim = c(0.4, ymax + 0.6)) +
coord_cartesian(xlim = c(0.4, xmax + 0.6)) +
scale_x_continuous(breaks=seq(1,xmax,1))+
scale_y_continuous(breaks=seq(1,ymax,1))+
theme(axis.line = element_line(colour = "white"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank())
which gives
The desplot package will do this for you (using lattice):
library(desplot)
desplot(cat ~ xpos*ypos, testData, out1=blocknr, text=blocknr, main="testData")