beth_9
beth_9

Reputation: 113

How to run a Linear Regression in R with a dataset from BigQuery?

I connected BigQuery Dataset with RStudio by running the following lines of code in RStudio.

install.packages("bigrquery")

library(bigrquery)
library(DBI)

con <- dbConnect(
  bigrquery::bigquery(),
  project = "MyProjectID",
  dataset = "MyDataset"
)
test<- dbGetQuery(con, sql, n = 10000, max_pages = Inf)


sql <- "SELECT * FROM  `MyProjectID.MyDataset`" 

tb <- bigrquery::bq_project_query("MyProjectID", sql)
bq_table_download(tb, max_results = 10)

And now, I wanted to run a linear regression analysis. I used the following to build a regression model.

model_fit = lm(event_n ~ geo_country,
           data = "`MyProjectID.MyDataset`")

But it's giving me the error, "'data' must be a data.frame, environment, or list"

Could anyone help me fix this? Thanks a lot!

Upvotes: 0

Views: 112

Answers (1)

neilfws
neilfws

Reputation: 33772

You need to assign the result from bq_table_download to a data frame variable. For example:

mydata <- bq_table_download(tb)

And then:

model_fit = lm(event_n ~ geo_country,
               data = mydata)

See this guide.

Upvotes: 1

Related Questions