Error in svytotal() : could not find function "svytotal"

I am close to running the script successfully and I got this error in the final script. I am running it on the Rstudio cloud.I computed it by referring PDF: 2020 microdata file to compute estimates and standard errors (RSEs).Page 6,7 in the webpage survey link Here is my complete script:

# Ref: file:///C:/Users/MMatam/OneDrive%20-%20University%20of%20Central%20Florida/Projects/20230123_US_EIA_DataAnalysis/Residential_BatteryPV_ElectricVehicle_MM/ResidentialEnergyConsumptionSurvey_RECS/microdata-guide.pdf
install.packages("survey")
library(survey)
# Ref: https://stackoverflow.com/questions/54621706/error-in-librarydplyr-there-is-no-package-called-dplyr
install.packages('dplyr')
library(dplyr)
# Import the CSV file from local machine
# Ref: https://forum.posit.co/t/how-can-i-upload-csv-or-excel-files-existing-in-computer-to-rstudio-cloud/23621
# To import the csv again into this space, right click on the file name and click import dataset
recs2020 <- read_csv(file="recs2020_public_v1.csv")
# Read the 
recs2020$NG_MAINSPACEHEAT <- ifelse(recs2020$FUELHEAT == 1, 1, 0)
# 
repweights<-select(recs2020,NWEIGHT1:NWEIGHT60)
#
RECS <- svrepdesign(data = recs2020,
                    weight = ~NWEIGHT,
                    repweights = repweights,
                    type = "JK1",
                    combined.weights = TRUE,
                    scale = (ncol(repweights)-1)/ncol(repweights),
                    mse = TRUE)
#
NG_MAINSPACEHEAT<-as.data.frame(svytotal(~NG_MAINSPACEHEAT,RECS))

Present output:

Error in svytotal(~NG_MAINSPACEHEAT, RECS) : 
  could not find function "svytotal"

Hi,

Welcome to the RStudio community!

When I run your script it produces no errors. The fact that svrepdesign runs should mean that the svytotal function is present as well. Try to run the script without installing the packages every time (only needs to be done once, then remove it) and with a clean environment.

library(survey)
library(dplyr)

recs2020 = read.csv("https://www.eia.gov/consumption/residential/data/2020/csv/recs2020_public_v1.csv")

recs2020$NG_MAINSPACEHEAT <- ifelse(recs2020$FUELHEAT == 1, 1, 0)

repweights<-select(recs2020,NWEIGHT1:NWEIGHT60)

RECS <- svrepdesign(data = recs2020,
                   weight = ~NWEIGHT,
                   repweights = repweights,
                   type = "JK1",
                   combined.weights = TRUE,
                   scale = (ncol(repweights)-1)/ncol(repweights),
                   mse = TRUE)

NG_MAINSPACEHEAT<-as.data.frame(svytotal(~NG_MAINSPACEHEAT,RECS))

Hope this helps,
PJ

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.