Trying to understand rlang sym(id) error In R - r

I wonder how I can sort this bug in R.
My simple lines
Remit_data <- panel_data(dataremit, id = id, wave = t)
model<-asym(wel_loggdp_cap ~ logremit + remitsq + logcpi + corruption +
employilo + senrol_netprim + logfert + urbanization + tradegdp +
netoda_gini, data = dataremit)
I get this error
Error: Only strings can be converted to symbols Backtrace:
panelr::asym(...)
panelr:::diff_data(...)
rlang::sym(id)

In panelr you have to define your panel data classifier (e.g. id / time) outside of the wmb function. You can compare this to plm were it can be done within plm.
library(panelr)
library(plm)
data(Produc)
# fixed effects with plm
FE_plm <- plm(gsp ~ pcap + pc + pcap:pc,
data = Produc,
index = c("state","year"),
method="within")
# fixed effects with panelr
Produc <- panel_data(Produc, id = state, wave = year)
FE_panelr <- wbm(gsp ~ pcap + pc + pcap:pc,
model = "within",
interaction.style = c("double-demean"),
data = Produc)
This should fix the issue. Always try to provide a minimal working example.

Related

boxCox Error in model.frame.default : 'data' must be a data.frame, environment, or list

This data is from an excel CSV file.
I want to see if a transformation is necessary, but my problem is that I keep getting this message:
Error in model.frame.default(formula = comment$Number.of.Comments ~ comment$Character.Count + : 'data' must be a data.frame, environment, or list
The following is my code:
comment <- read.csv('AdAnalysis3.csv', header = TRUE, fileEncoding = "UTF-8-BOM")
commentfit <- lm(comment$Number.of.Comments ~ comment$Character.Count + comment$Number.of.Shares + comment$Number.of.Likes + comment$Type.of.Ad + comment$Dealing.with.Life + comment$Christlike.Attributes + comment$Spiritual.Learning, data = comment)
library(car)
boxCox(commentfit)
I get the following message immediately after boxCox(commentfit):
Any suggestions?
You haven't given us a reproducible example, but my guess is that you have confused car::boxCox() by including comment$ in your formula. In general it's better (for a number of reasons including clarity) to specify a linear model with just the variable names, i.e.:
commentfit <- lm(Number.of.Comments ~ Character.Count + Number.of.Shares +
Number.of.Likes + Type.of.Ad + Dealing.with.Life +
Christlike.Attributes + Spiritual.Learning,
data = comment)

How to implement your own nonlinear function in nlmer in R?

I am trying to implement a new nonlinear function to use in nlmer function in lme4 package. But I'm not sure what the problem is. This is the first time I'm trying to use nlmer but I'm following all the instructions I've found on the internet. The first error is about my dataframe.
data <- read.csv(paste("C:/Users/oguz/Desktop/Runs4SiteModels/db/", "DB4NLSiteModel", Periods[i],".txt", sep=""), sep = "", header = TRUE)
psa_rock <- data$PSAr
nparams <- c("c")
nonl_fn <- deriv(~ log(( psa_rock + c)/c),
namevec = c("c"),
function.arg=c("c", psa_rock))
fm <- nlmer(log(data$PSAm) ~ nonl_fn(c, psa_rock) ~ 1 + data$M1 + data$M3 + data$M85 + data$Nflag + data$Rflag + data$FDepth +
data$Dist1 + data$Dist3 + data$VN + (exp(-1*exp(2*log(data$Vs)- 11)) * log((data$PSAr + c) / c) ) +
(1|data$EQID) + (1|data$STID), data=data, start=c(c=0.1))
When I run this code, I'm getting the following error:
Error in model.frame.default(data = data, drop.unused.levels = TRUE, formula = log(data$PSAm) ~ :
invalid type (list) for variable 'data'
which I wasn't getting it while using lmer function (of course without the nonlinear function). That's why I'm thinking my problem is not about my dataframe.
Other issue that I couldn't stop thinking about, the part in the fixed-effects:
(exp(-1*exp(2*log(data$Vs)- 11)) * log((data$PSAr + c) / c) )
as you can see my nonlinear function also takes a part in my fixed-effects formula and I'm not quite sure how to implement that. I hope my way is correct but because of my first problem, I couldn't find an opportunity to test that.

How to declare a for loop in R

I am trying to create a simple for loop in R, but I am not sure how to go about this without creating a global variable.
I am trying to output a predict table neatly, instead of running code through many different instances (something like below) that I wish to predict.
house1 = newdata[1,]
predict(fullmodel, house1)
predict(sqftmodel, house1)
predict(bestmodel, house1)
house2 = newdata[2,]
predict(fullmodel, house2)
predict(sqftmodel, house2)
predict(bestmodel, house2)
house3 = newdata[3,]
I want to use a for loop to run through 37 different houses and have the output in a table. Any ideas?
edit: this is a portion of my code so far
data = read.table("DewittData.txt")
newdata = na.omit(data)#28 points to refer to
colnames(newdata) = c("ListPrice", "Beds", "Bath","HouseSize","YearBuilt"
,"LotSize", "Fuel","ForcedAir", "Other","FM","ESM","JD",
"SchoolDistrict","HouseType","GarageStalls","Taxes")
attach(newdata)
fullmodel = lm((ListPrice) ~ HouseSize + Beds + Bath + YearBuilt + LotSize
+ Fuel + ForcedAir + Other + SchoolDistrict+
HouseType + Other + FM + ESM + JD + GarageStalls + Taxes)
bestmodel = lm(ListPrice~Beds)
sqftmodel = lm(ListPrice~HouseSize, data = newdata)
update:
I see, so I've changed it to
predict(fullmodel, newdata[,])
predict(sqftmodel, newdata[,])
predict(bestmodel, newdata[,])
Now how would I output this in a table format?
I am not sure if I get your question , but this what I would do for predicting based on different rows of a df.
Housefull <- predict(fullmodel, newdata[,])
Housebest <- predict(bestmodel, newdata[,])
Housesqft <- predict(sqftmodel, newdata[,])
Generally, sticking to vectors is much better than using loops.

Creating lavaan and mirt models permuations of varying size and length

hoping someone can offer some guidance here.
I'm creating a multivariate simulation using the simDesign package, I am varying the number of factors as well as items that load on each factor. I would like to write a command that identifies the number of factors present in factornumbers and assigns the appropriate items to them (no cross loading). I will be testing all combinations of the conditions below and more, and I would like to have a model command that acknowledge the iterations of differing models, so I don't have to write multiple model statements.
factornumbers<-c(1,2,3,5)
itemsperfactor<-c(5,10,30)
What lavaan and mirt are looking for is below:
mirtmodel<-mirt.model('
F1=1-15
F2=16-30
MEAN=F1,F2
COV=F1*F2')
lavmodel <- ' F1=~ Item_1 + Item_2 + Item_3 + Item_4 + Item_5 + Item_6 + Item_7 + Item_8 + Item_9 + Item_10 + Item_11 + Item_12 + Item_13 + Item_14 + Item_15
F2=~ Item_16 + Item_17 + Item_18 + Item_19 + Item_20 + Item_21 + Item_22 + Item_23 + Item_24 + Item_25 + Item_26 + Item_27 + Item_28 + Item_29 + Item_30'
The simDesign package offers this example, I would like to expand on it but I'm not sure I have the know-how:
lavmodel<-paste0('F=~ ', paste0(colnames(dat)[1L], ' + '),
paste0(colnames(dat)[-1L], collapse = ' + '))
What I would like is a single mirt and lavaan command that finds the number of factors specified in the factornumbers command and assigns the correct items specified in the data as well as itemsperfactor.
EDIT:
I would like the model identification to pick up on which factor & item structure is in use for that condition and fill in the model identification with the correct information.
For Example:
mirtmodel<-mirt.model('
F1=1-1
F2=6-10
F3=11-15
F4=16-20
F5=21-25
MEAN=F1,F2,F3,F4,F5
COV=F1*F2*F3*F4*F5')
Or
mirtmodel<-mirt.model('
F1=1-30
F2=31-60
MEAN=F1,F2
COV=F1*F2')
And also the corresponding lavaan models.
The idea here is to paste different strings together so that the condition input (row of the respective Design object) is all that is required to construct a suitable model specification string. Generating syntax for simulations is arguably the most annoying part of simulations, but at least in R there are a good number of helpful string operations (plus, packages like stringr).
Here's my interpretation of what you are currently looking for using base R functions.
library(SimDesign)
library(mirt)
Design <- createDesign(factornumbers = c(1,2,3,5),
itemsperfactor = c(5,10,30))
gen_syntax_mirt <- function(condition){
fn <- with(condition, factornumbers)
ipf <- with(condition, itemsperfactor)
nitems <- fn * ipf
maxloads <- sort(seq(nitems, ipf, length.out = fn))
minloads <- c(1, maxloads[-length(maxloads)] + 1)
fnames <- paste0('F', 1:fn)
df <- cbind(fnames, ' = ', minloads, '-', maxloads)
s1 <- apply(df, 1, paste0, collapse = '')
s2 <- paste0('MEAN = ', paste0(fnames, collapse = ','))
s3 <- paste0('COV = ', paste0(fnames, collapse = '*'))
ret <- paste0(c(s1, s2, s3), collapse = '\n')
mirt.model(ret)
}
gen_syntax_mirt(Design[1,])
gen_syntax_mirt(Design[10,])
The input to this function is a single row from the Design input to runSimulation(), so you can see here that it will work just fine. Do something similar for lavaan's syntax and you'll be set.

number of rows in use has changed: remove missing values?

I have been trying to do stepwise selection on my variables with R. This is my code:
library(lattice)#to get the matrix plot, assuming this package is already installed
library(ftsa) #to get the out-of sample performance metrics, assuming this package is already installed
library(car)
mydata=read.csv("C:/Users/jgozal1/Desktop/Multivariate Project/Raw data/FINAL_alldata_norowsunder90_subgroups.csv")
names(mydata)
str(mydata)
mydata$country_name=NULL
mydata$country_code=NULL
mydata$year=NULL
mydata$Unemployment.female....of.female.labor.force...modeled.ILO.estimate.=NULL
mydata$Unemployment.male....of.male.labor.force...modeled.ILO.estimate.=NULL
mydata$Life.expectancy.at.birth.male..years.= NULL
mydata$Life.expectancy.at.birth.female..years. = NULL
str(mydata)
Full_model=lm(mydata$Fertility.rate.total..births.per.woman. + mydata$Immunization.DPT....of.children.ages.12.23.months. + mydata$Immunization.measles....of.children.ages.12.23.months. + mydata$Life.expectancy.at.birth.total..years. + mydata$Mortality.rate.under.5..per.1000.live.births. + mydata$Improved.sanitation.facilities....of.population.with.access. ~ mydata$Primary.completion.rate.female....of.relevant.age.group. + mydata$School.enrollment.primary....gross. + mydata$School.enrollment.secondary....gross. + mydata$School.enrollment.tertiary....gross. + mydata$Internet.users..per.100.people. + mydata$Primary.completion.rate.male....of.relevant.age.group. + mydata$Mobile.cellular.subscriptions..per.100.people. + mydata$Foreign.direct.investment.net.inflows..BoP.current.US.. + mydata$Unemployment.total....of.total.labor.force...modeled.ILO.estimate., data= mydata)
summary(Full_model) #this provides the summary of the model
Reduced_model=lm(mydata$Fertility.rate.total..births.per.woman. + mydata$Immunization.DPT....of.children.ages.12.23.months. + mydata$Immunization.measles....of.children.ages.12.23.months. + mydata$Life.expectancy.at.birth.total..years. + mydata$Mortality.rate.under.5..per.1000.live.births. + mydata$Improved.sanitation.facilities....of.population.with.access. ~1,data= mydata)
step(Reduced_model,scope=list(lower=Reduced_model, upper=Full_model), direction="forward", data=mydata)
step(Full_model, direction="backward", data=mydata)
step(Reduced_model,scope=list(lower=Reduced_model, upper=Full_model), direction="both", data=mydata)
This is the link to the dataset that I am using: http://speedy.sh/YNXxj/FINAL-alldata-norowsunder90-subgroups.csv
After setting the scope for my stepwise I get this error:
Error in step(Reduced_model, scope = list(lower = Reduced_model, upper = Full_model), :
number of rows in use has changed: remove missing values?
In addition: Warning messages:
1: In add1.lm(fit, scope$add, scale = scale, trace = trace, k = k, :
using the 548/734 rows from a combined fit
2: In add1.lm(fit, scope$add, scale = scale, trace = trace, k = k, :
using the 548/734 rows from a combined fit
I have looked at other posts with the same error and the solutions usually is to omit the NAs from the data used, but that hasn't solved my problem and I am still getting exactly the same error.

Resources