Regression Error- Please help (new R user)

I am receiving this error:

Error in eval(predvars, data, env) :
object 'incumbentvoteshare' not found

Here is my code so far
library(tidyverse)
library(modelsummary)
spend = read.csv("HouseElectionsSpending2018.csv")
data <- read.csv("HouseElectionsSpending2018.csv")
spend = spend |>
mutate(demvoteshare = 1 - repvoteshare,
incumbentvoteshare = case_when(incumbent == "R" ~ repvoteshare,
incumbent == "D" ~ demvoteshare)
)
spend = spend |>
mutate(demspending = 1 - repspending,
incumbentspending = case_when(incumbent == "R" ~ repvoteshare,
incumbent == "D" ~ demvoteshare)
)

And here is the one I am trying to run when I get the error:
model_incumbent <- lm(incumbentvoteshare ~ incumbentspending, spend = spend)
summary(model_incumbent)

Think you want data=spend rather than spend=spend.

1 Like

Should be incumbentvoteshare

I'm getting the same error on this code:

model_challenger <- lm(challengervoteshare ~ challengerspending, data = spend)
summary(model_challenger)

here is the error: Error in eval(predvars, data, env) :
object 'challengervoteshare' not found

Here is the info on the variables. I dont think I am using the mutate correctly which is causing issues with the regression

This note explains the contents of the data file HouseElectionSpending2018 (available in csv or dta format).

Each row corresponds to a U.S. House election in 2018.

The sample is restricted to contested elections between an incumbent and a challenger.

Summary of variables
state: state postal code

dist: district number

incumbent: "R" if the incumbent is a Republican, "D" if the incumbent is a Democrat

repvoteshare: the Republican candidate's two-party vote share (i.e., Republican votes/(Republican votes + Democratic votes) in 2018

repspending: a measure of the campaign spending of the Republican (specifically, the natural logarithm of dollars spent plus 1)

demspending: a measure of the campaign spending of the Democrat (specifically, the natural logarithm of dollars spent plus 1)

(Note: because campaign spending is measured as the natural logarithm of dollars spent, we approximately interpret a one-unit
increase in these variables as a doubling of campaign spending)

trumpvoteshare: the two-party vote share of Donald Trump (the Republican) in the 2016 presidential election in that district

lagrepvoteshare: the Republican candidate's two-party vote share in the previous House election (in 2016)

here is the info on the variables- I dont think I am using mutate correctly:

This note explains the contents of the data file HouseElectionSpending2018 (available in csv or dta format).

Each row corresponds to a U.S. House election in 2018.

The sample is restricted to contested elections between an incumbent and a challenger.

Summary of variables
state: state postal code

dist: district number

incumbent: "R" if the incumbent is a Republican, "D" if the incumbent is a Democrat

repvoteshare: the Republican candidate's two-party vote share (i.e., Republican votes/(Republican votes + Democratic votes) in 2018

repspending: a measure of the campaign spending of the Republican (specifically, the natural logarithm of dollars spent plus 1)

demspending: a measure of the campaign spending of the Democrat (specifically, the natural logarithm of dollars spent plus 1)

(Note: because campaign spending is measured as the natural logarithm of dollars spent, we approximately interpret a one-unit
increase in these variables as a doubling of campaign spending)

trumpvoteshare: the two-party vote share of Donald Trump (the Republican) in the 2016 presidential election in that district

lagrepvoteshare: the Republican candidate's two-party vote share in the previous House election (in 2016)

Where is this variable defined?

I think I'm supposed to know that as the opposite of incumbent? I have no idea! Hence the problem here.

Perhaps you want to define challengervoteshare as 1-incumbentvoteshare?

Also note tht you ahve defined incumbentspending as coming from vote share not spending.

1 Like

Variables are either read in from an external source, such as a CSV file, or they are created by defining them, either directly

foo <- β€œbar”

or more indirectly like

NewData <- MyData |> mutate(foo = 42)

Without being able to trace this, troubleshooting will be frustrating. Also, interpreting results of whatever analysis is being attempted will be problematic.

I still get the error (Error in eval(predvars, data, env) :
object 'challengervoteshare' not found):

data = spend |>
mutate(challengervoteshare = 1 - incumbentvoteshare)

data = spend |>
mutate(challengerspending = 1 - incumbentspending)

model_challenger <- lm(challengervoteshare ~ challengerspending, data = spend)
summary(model_challenger)

You've updated a dataframe named data instead of spend.

1 Like

Thanks for your help! Any idea how I would create a whole new variable indicating if the incumbent is a Republican called republicanincumbent?
It has to take the value of 1 if the incumbent is a Republican and 0 if the incumbent is a Democrat.

spend<- spend |> mutate(republicanincumbent = ifelse(incumbent == "R",1,0))

1 Like

This topic was automatically closed 21 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.