Ruggiero Rippo
Ruggiero Rippo

Reputation: 17

Error in coefficients * c(x["price"], as.numeric(x["taste"] == "Sour" | : non-numeric argument to binary operator

In the following code i get the error Error in coefficients * c(x["price"], as.numeric(x["taste"] == "Sour" | : non-numeric argument to binary operator

I tried to do x$price but does not work for atomic vector. Any suggestions to work around this

# Set the seed for reproducibility
set.seed(123)

# Create a design matrix with different levels for each factor
price <- rep(c(1, 2, 3), each = 4)
taste <- rep(c("Sweet", "Sour", "Salty", "Spicy"), times = 3)
texture <- rep(c("Crunchy", "Smooth"), each = 6)

design_matrix <- data.frame(price, taste, texture)

# Generate random utility coefficients for each factor
coefficients <- c(-0.5, -0.3, 0.8)

# Calculate the utility of each food option for each combination of factors
utility <- apply(design_matrix, 1, function(x) {
  sushi_utility <- sum(coefficients * c(x["price"], 
                                        as.numeric(x["taste"] == "Sour" | x["taste"] == "Salty"), 
                                        as.numeric(x["texture"] == "Smooth")))
  pizza_utility <- sum(coefficients * c(x["price"], 
                                        as.numeric(x["taste"] == "Sweet" | x["taste"] == "Spicy"), 
                                        as.numeric(x["texture"] == "Crunchy")))
  burger_utility <- sum(coefficients * c(x["price"], 
                                         as.numeric(x["taste"] == "Salty" | x["taste"] == "Spicy"), 
                                         as.numeric(x["texture"] == "Crunchy")))
  
  # Calculate the probability of choosing each food option
  probabilities <- exp(c(sushi_utility, pizza_utility, burger_utility))
  probabilities <- probabilities / sum(probabilities)
  
  # Simulate the choice of food option based on the probabilities
  food <- sample(c("Sushi", "Pizza", "Burger"), size = 1, prob = probabilities)
  
  # Return a data frame with the factors and the chosen food option
  data.frame(price = x["price"], taste = x["taste"],
             texture = x["texture"], food = food)
})

# Print the first few rows of the simulated dataset
head(utility)

I tried to use x$price but does not work for atomic vector

Upvotes: 0

Views: 17

Answers (1)

Ben Bolker
Ben Bolker

Reputation: 226741

From ?apply:

If ‘X’ is not an array but an object of a class with a non-null ‘dim’ value (such as a data frame), ‘apply’ attempts to coerce it to an array via ‘as.matrix’ if it is two-dimensional (e.g., a data frame) or via ‘as.array’.

that means that the price variable gets coerced to a character (because columns of matrices must all be the same type, and you have a couple of character columns in the data frame)

head(as.matrix(design_matrix), 2)
     price taste   texture  
[1,] "1"   "Sweet" "Crunchy"
[2,] "1"   "Sour"  "Crunchy"

There's probably a better way around this, but you could start your function by converting price back to numeric, e.g. ...

utility <- apply(design_matrix, 1, function(x) {
    price <- as.numeric(x["price"])
    ...
}

Upvotes: 1

Related Questions