Run Multiple Regression from Business Manager

I'm trying to run Multiple regressions from metrics pulled out from Facebook Business Manager. However, since I'm not an experiences R user I have problems with converting data from the Excel report in order to be able to run a regression model.

I'd be really happy if someone is able to solve the struggles I have here ↓

my_data <- read.csv(file = "clipboard",
                    sep = "\t", header=TRUE)

#I can see here that my data have different classes. E.g. some are integers and some are characters 
lapply(my_data,class)


#Trying to convert all to numeric
my_data <- as.numeric(as.character(my_data))

#getting now the error
Warning message:
NAs introduced by coercion 

I have been trying to first convert all to factors e.g.:
my_data <- read.csv(file = "clipboard",
sep = "\t", header=TRUE,
colClasses = "factor")

But here when trying to convert
my_data <- as.numeric(as.factor(my_data))

Then I almost all of my data removed.

I will eventually like to be able to run simple models as:

#Navngiver vores datasæt: 
"ROAS" <- my_data$Purchase.ROAS..Return.on.Ad.Spend.
"Clicks" <- my_data$Link.Clicks
"AmountSpent" <- my_data$Amount.Spent..DKK.
"PostReactions" <- my_data$Post.Reactions


#model
ROASModel <- lm(ROAS ~ PostReactions + AmountSpent)


#stargazer
stargazer(ROASModel, type = "text")

It sounds like we need to see some of your data. It looks like you are trying doing too many data conversions. This command as.factor(my_data) looks very strange.

Can you point us to it at the Facebook Business Manager or supply the data you have in the data.frame my__data?

A very simple and very effective way to supply some data is to use the dput() command.

dput(mydata)

and then simply copy the output and paste it here. If you have a very large data set then a sample should be fine. To supply us with 100 rows of your data set do

dput(head(mydata , 100))

where mydata is the name of your dataframe or tibble

Hey - of course. I have the data here using dput ↓


structure(list(Amount.Spent..DKK. = c("49746", "23180,5", "69364", 
"8362", "1212", "3201,2", "81126", "5028,21", "13509", "10500", 
"5357,1", "60987", "5600", "2010", "109549", "12000", "28887", 
"2773,42", "3708", "22862", "12000,86", "1383", "1856,4", "1063,6", 
"6000,4", "2740", "9473", "6545", "5567,31", "7173", "42227,1", 
"239793", "2655", "60590", "159934,45", "5944", "1675", "3377", 
"3015", "10677", "2651,1", "5986", "41022", "4466,92", "8501", 
"7809,5", "24143", "10517", "6228", "9987", "17241", "2274", 
"33428", "4400", "12235", "1109", "41641", "4677", "5367", "4503", 
"5794", "5000", "45545", "11766", "5674"), Post.Reactions = c(1276L, 
246L, 798L, 196L, 17L, 75L, 98L, 131L, 15L, 305L, 127L, 842L, 
16L, 2L, 1266L, 196L, 515L, 28L, 31L, 133L, 107L, 7L, 39L, 531L, 
62L, 41L, 210L, 113L, 35L, 152L, 430L, 8055L, 57L, 348L, 3589L, 
1229L, 27L, 54L, 26L, 168L, 33L, 90L, 12L, 42L, 280L, 93L, 111L, 
102L, 32L, 254L, 428L, 40L, 56L, 4L, 25L, 8L, 135L, 29L, 167L, 
13L, 199L, 10L, 522L, 200L, 187L), Post.Engagement = c(4755L, 
1631L, 16565L, 4273L, 158L, 5993L, 122725L, 999L, 22936L, 1132L, 
2987L, 11979L, 20936L, 3383L, 14215L, 984L, 2700L, 410L, 915L, 
72960L, 644L, 2094L, 315L, 634L, 1387L, 329L, 1475L, 428L, 351L, 
1247L, 3576L, 33077L, 275L, 6062L, 462781L, 5198L, 207L, 10659L, 
11715L, 26359L, 148L, 831L, 5131L, 12824L, 2680L, 705L, 767L, 
882L, 5604L, 1437L, 33763L, 304L, 28889L, 199L, 11296L, 4728L, 
1341L, 543L, 570L, 268L, 674L, 318L, 4242L, 21301L, 667L), Link.Clicks = c(3254L, 
1359L, 15565L, 2624L, 134L, 1471L, 5250L, 858L, 965L, 798L, 2818L, 
10834L, 759L, 163L, 12731L, 767L, 2147L, 378L, 879L, 2745L, 524L, 
64L, 269L, 86L, 1296L, 284L, 1211L, 302L, 312L, 1047L, 3016L, 
22969L, 200L, 5670L, 21773L, 3815L, 177L, 255L, 175L, 597L, 110L, 
720L, 1114L, 176L, 2318L, 580L, 647L, 751L, 432L, 1152L, 1934L, 
249L, 702L, 195L, 226L, 233L, 1188L, 507L, 393L, 249L, 455L, 
302L, 3656L, 1199L, 477L), Adds.to.Cart = c(1077L, 205L, 262L, 
127L, 6L, 112L, 528L, 12L, 120L, 82L, 32L, 301L, 39L, 3L, 641L, 
67L, 115L, 4L, 14L, 202L, 55L, 8L, 2L, 1L, 12L, 5L, 73L, 25L, 
5L, 29L, 260L, 884L, 13L, 247L, 549L, 46L, 3L, 55L, 6L, 24L, 
20L, 1L, 14L, 5L, 3L, 14L, 109L, 31L, 22L, 103L, 94L, 12L, 176L, 
49L, 35L, 26L, 182L, 12L, 72L, 22L, 22L, 14L, 322L, 57L, 22L), 
    Purchases = c(430L, 105L, 132L, 7L, 3L, 38L, 225L, 9L, 53L, 
    37L, 13L, 97L, 11L, 5L, 251L, 32L, 39L, 2L, 8L, 48L, 32L, 
    1L, 1L, 2L, 6L, 3L, 16L, 8L, 3L, 1L, 60L, 379L, 1L, 89L, 
    217L, 2L, 2L, 2L, 2L, 5L, 6L, 2L, 4L, 2L, 1L, 1L, 64L, 11L, 
    8L, 15L, 32L, 1L, 72L, 9L, 21L, 6L, 68L, 7L, 22L, 9L, 13L, 
    11L, 123L, 22L, 5L), Purchase.ROAS..Return.on.Ad.Spend. = c("9,656615", 
    "4,546138", "2,133519", "0,781939", "2,773046", "14,435083", 
    "2,930033", "1,681652", "4,100064", "3,79113", "3,284675", 
    "1,568252", "2,390962", "2,798869", "2,374608", "2,748595", 
    "1,424031", "0,597996", "2,116865", "2,027512", "2,455917", 
    "0,635595", "0,430562", "1,579433", "0,919183", "0,878725", 
    "1,840102", "1,164193", "0,467641", "0,114018", "1,908024", 
    "2,090611", "0,451888", "1,668044", "1,698147", "0,404363", 
    "1,397824", "0,266186", "0,706232", "0,555856", "2,999532", 
    "0,317049", "1,115068", "0,402627", "0,225008", "0,208062", 
    "2,974487", "1,140982", "1,334519", "1,582212", "1,975065", 
    "0,316254", "2,002542", "2,14102", "1,499612", "0,93668", 
    "1,286902", "1,624873", "3,256563", "2,136109", "1,31595", 
    "2,161382", "2,426768", "1,724376", "0,853213")), class = "data.frame", row.names = c(NA, 
-65L))

First silly question, is the "," a decimal separator or a thousands separator?

It seems to be turning the first variable Amount.Spent..DKK.
and the last variable * Purchase.ROAS..Return.on.Ad.Spend.* into character variables which does not look like what is intended.

I converted your data to decimal separator "." and all seems well but I did not try it in stargazer.

# rename columns to something simple
names(my_data)  <-   c("amt", "react", "engage", "clicks", "cart", "purchase", "roas")

# crude but effective way to convert to . vs , as decimal seperator. There must be a better way!
write.csv2(my_data, "~/RJunk/mount/mntdata1.csv", row.names = FALSE)

my_data  <-  read.csv2("~/RJunk/mount/mntdata1.csv")

ROASModel <- lm(roas ~ react + amt, data = my_data)
 
ROASModel

Thanks, the "," is a decimal seperator :slight_smile:

Good, looks like we are in business. I have very seldomly dealt with comma delimited variables as you can probably see from my crappy code. Here is a link to an article you may find useful.

Best wishes,
John

1 Like

Hi, I would be very interested to know if you were able to solve your problem.

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.