how to exclude variables (columns) from an R - r

This is the R code for logistic reg model,
> hrlogis1 <- glm(Attrition~. -Age -DailyRate -Department -Education
> -EducationField -HourlyRate -JobLevel
> -JobRole -MonthlyIncome -MonthlyRate
> -PercentSalaryHike -PerformanceRating
> -StandardHours -StockOptionLevel
> , family=binomial(link = "logit"),data=hrtrain)
where:
Attrition is the dependent variable and rest are all the independent variables.
Below is the summary of the model:
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 1.25573 0.84329 1.489 0.136464
BusinessTravelTravel_Frequently 1.86022 0.47410 3.924 8.72e-05 ***
BusinessTravelTravel_Rarely 1.28273 0.44368 2.891 0.003839 **
DistanceFromHome 0.03869 0.01138 3.400 0.000673 ***
EnvironmentSatisfaction -0.36484 0.08714 -4.187 2.83e-05 ***
GenderMale 0.52556 0.19656 2.674 0.007499 **
JobInvolvement -0.59407 0.13259 -4.480 7.45e-06 ***
JobSatisfaction -0.37315 0.08671 -4.303 1.68e-05 ***
MaritalStatusMarried 0.23408 0.26993 0.867 0.385848
MaritalStatusSingle 1.37647 0.27511 5.003 5.63e-07 ***
NumCompaniesWorked 0.16439 0.04034 4.075 4.59e-05 ***
OverTimeYes 1.67531 0.20054 8.354 < 2e-16 ***
RelationshipSatisfaction -0.23865 0.08726 -2.735 0.006240 **
TotalWorkingYears -0.12385 0.02360 -5.249 1.53e-07 ***
TrainingTimesLastYear -0.15522 0.07447 -2.084 0.037124 *
WorkLifeBalance -0.30969 0.13025 -2.378 0.017427 *
YearsAtCompany 0.06887 0.04169 1.652 0.098513 .
YearsInCurrentRole -0.10812 0.04880 -2.216 0.026713 *
YearsSinceLastPromotion 0.14006 0.04452 3.146 0.001657 **
YearsWithCurrManager -0.09343 0.04984 -1.875 0.060834 .
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Now I want to remove those which are not significant, here in this case "MaritalStatusMarried" is not significant.
MaritalStatus is a variable(column) with two levels "Married" and "Single".

How about:
data$MaritalStatus[data[,num]="Married"] <- NA
(where num = number of the column in the data)
The values for Married will be replaced for NA's and then you can run the glm model again.

Related

Relative Survival with Relsurv-R

How can I estimate the RER and the confidence intervals from the additive 5-years relative survival model?
I used the following syntax:
model3<- rsadd(Surv(durata_days,status_cat)~agediag_cat+sex+country,
ratetable=rt,data=nodco,
rmap=list(age=agediag*365.241, year=year(dtnewdiag),sex=sex, country=country), int = 5, method="glm.poi")
summary(model3)
Call:
rsadd(formula = Surv(durata_days, status_cat) ~ agediag_cat +
sex + country, data = nodco, ratetable = rt, int = 5, method = "glm.poi",
rmap = list(age = agediag * 365.241, year = year(dtnewdiag),
sex = sex, country = country))
Coefficients:
Estimate Std. Error z value Pr(>|z|)
agediag_cat55-69 0.08195 0.05134 1.596 0.11048
agediag_cat>=70 0.42853 0.05053 8.480 < 2e-16 ***
sexfemale -0.21838 0.04065 -5.372 7.77e-08 ***
countryEstonia 0.18457 0.06438 2.867 0.00415 **
countryPortugal 0.09580 0.05700 1.681 0.09283 .
countrySpain 0.16414 0.05742 2.859 0.00425 **
countrySwitzerland -0.19424 0.06686 -2.905 0.00367 **
fu [0,1] -0.26606 0.06715 -3.962 7.43e-05 ***
fu (1,2] -0.96752 0.07516 -12.873 < 2e-16 ***
fu (2,3] -1.44282 0.08988 -16.053 < 2e-16 ***
fu (3,4] -1.80198 0.12497 -14.419 < 2e-16 ***
fu (4,5] -2.20702 0.18353 -12.026 < 2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’
I tried the following code:
round(exp(cbind(RR = coef(model3), confint(model3))),3)
Waiting for profiling to be done...
Error in `[<-`(`*tmp*`, , names(coef(fm)), value = coef(fm)) :
subscript out of bounds

Changing base category in latent class analysis

I'm using the glca package to run a latent class analysis. I want to see how covariates (other than indicators used to construct latent classes) affect the probability of class assignment. I understand this is a multinomial logistic regression, and thus, my question is, is there a way I can change the base reference latent class? For example, my model is currently a 4-class model, and the output shows the effect of covariates on class prevalence with respect to Class-4 (base category) as default. I want to change this base category to, for example, Class-2.
My code is as follows
fc <- item(intrst, respect, expert, inclu, contbt,secure,pay,bonus, benft, innov, learn, rspons, promote, wlb, flex) ~ atenure+super+sal+minority+female+age40+edu+d_bpw+d_skill
lca4_cov <- glca(fc, data = bpw, nclass = 4, seed = 1)
and I get the following output.
> coef(lca4_cov)
Class 1 / 4 :
Odds Ratio Coefficient Std. Error t value Pr(>|t|)
(Intercept) 1.507537 0.410477 0.356744 1.151 0.24991
atenure 0.790824 -0.234679 0.102322 -2.294 0.02183 *
super 1.191961 0.175600 0.028377 6.188 6.29e-10 ***
sal 0.937025 -0.065045 0.035490 -1.833 0.06686 .
minority 2.002172 0.694233 0.060412 11.492 < 2e-16 ***
female 1.210653 0.191160 0.059345 3.221 0.00128 **
age40 1.443603 0.367142 0.081002 4.533 5.89e-06 ***
edu 1.069771 0.067444 0.042374 1.592 0.11149
d_bpw 0.981104 -0.019077 0.004169 -4.576 4.78e-06 ***
d_skill 1.172218 0.158898 0.036155 4.395 1.12e-05 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Class 2 / 4 :
Odds Ratio Coefficient Std. Error t value Pr(>|t|)
(Intercept) 3.25282 1.17952 0.43949 2.684 0.00729 **
atenure 0.95131 -0.04992 0.12921 -0.386 0.69926
super 1.16835 0.15559 0.03381 4.602 4.22e-06 ***
sal 1.01261 0.01253 0.04373 0.287 0.77450
minority 0.72989 -0.31487 0.08012 -3.930 8.55e-05 ***
female 0.45397 -0.78971 0.07759 -10.178 < 2e-16 ***
age40 1.26221 0.23287 0.09979 2.333 0.01964 *
edu 1.29594 0.25924 0.05400 4.801 1.60e-06 ***
d_bpw 0.97317 -0.02720 0.00507 -5.365 8.26e-08 ***
d_skill 1.16223 0.15034 0.04514 3.330 0.00087 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Class 3 / 4 :
Odds Ratio Coefficient Std. Error t value Pr(>|t|)
(Intercept) 0.218153 -1.522557 0.442060 -3.444 0.000575 ***
atenure 0.625815 -0.468701 0.123004 -3.810 0.000139 ***
super 1.494112 0.401532 0.031909 12.584 < 2e-16 ***
sal 1.360924 0.308164 0.044526 6.921 4.72e-12 ***
minority 0.562590 -0.575205 0.081738 -7.037 2.07e-12 ***
female 0.860490 -0.150253 0.072121 -2.083 0.037242 *
age40 1.307940 0.268453 0.100376 2.674 0.007495 **
edu 1.804949 0.590532 0.054522 10.831 < 2e-16 ***
d_bpw 0.987353 -0.012727 0.004985 -2.553 0.010685 *
d_skill 1.073519 0.070942 0.045275 1.567 0.117163
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
I would appreciate it if anyone let me know codes/references to address my problem. Thanks in advance.
Try using the decreasing option.
lca4_cov <- glca(fc, data = bpw, nclass = 4, seed = 1, decreasing = T)

Marginal Effects of conditional logit model in R using, "clogit," function

I am trying to figure out how to calculate the marginal effects of my model using the, "clogit," function in the survival package. The margins package does not seem to work with this type of model, but does work with "multinom" and "mclogit." However, I am investigating the affects of choice characteristics, and not individual characteristics, so it needs to be a conditional logit model. The mclogit function works with the margins package, but these results are widely different from the results using the clogit function, why is that? Any help calculating the marginal effects from the clogit function would be greatly appreciated.
mclogit output:
Call:
mclogit(formula = cbind(selected, caseID) ~ SysTEM + OWN + cost +
ENVIRON + NEIGH + save, data = atl)
Estimate Std. Error z value Pr(>|z|)
SysTEM 0.139965 0.025758 5.434 5.51e-08 ***
OWN 0.008931 0.026375 0.339 0.735
cost -0.103012 0.004215 -24.439 < 2e-16 ***
ENVIRON 0.675341 0.037104 18.201 < 2e-16 ***
NEIGH 0.419054 0.031958 13.112 < 2e-16 ***
save 0.532825 0.023399 22.771 < 2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Null Deviance: 18380
Residual Deviance: 16670
Number of Fisher Scoring iterations: 4
Number of observations: 8364
clogit output:
Call:
coxph(formula = Surv(rep(1, 25092L), selected) ~ SysTEM + OWN +
cost + ENVIRON + NEIGH + save + strata(caseID), data = atl,
method = "exact")
n= 25092, number of events= 8364
coef exp(coef) se(coef) z Pr(>|z|)
SysTEM 0.133184 1.142461 0.034165 3.898 9.69e-05 ***
OWN -0.015884 0.984241 0.036346 -0.437 0.662
cost -0.179833 0.835410 0.005543 -32.442 < 2e-16 ***
ENVIRON 1.186329 3.275036 0.049558 23.938 < 2e-16 ***
NEIGH 0.658657 1.932195 0.042063 15.659 < 2e-16 ***
save 0.970051 2.638079 0.031352 30.941 < 2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
exp(coef) exp(-coef) lower .95 upper .95
SysTEM 1.1425 0.8753 1.0685 1.2216
OWN 0.9842 1.0160 0.9166 1.0569
cost 0.8354 1.1970 0.8264 0.8445
ENVIRON 3.2750 0.3053 2.9719 3.6091
NEIGH 1.9322 0.5175 1.7793 2.0982
save 2.6381 0.3791 2.4809 2.8053
Concordance= 0.701 (se = 0.004 )
Rsquare= 0.103 (max possible= 0.688 )
Likelihood ratio test= 2740 on 6 df, p=<2e-16
Wald test = 2465 on 6 df, p=<2e-16
Score (logrank) test = 2784 on 6 df, p=<2e-16
margins output for mclogit
margins(model2A)
SysTEM OWN cost ENVIRON NEIGH save
0.001944 0.000124 -0.001431 0.00938 0.00582 0.0074
margins output for clogit
margins(model2A)
Error in match.arg(type) :
'arg' should be one of “risk”, “expected”, “lp”

Linear model regressing on every level of a numeric field

I am currently trying to run a linear model on a large data set, but am running into issues with some specific variables.
pv_model <- lm(SalePrice ~ MSSubClass + LotConfig + GarageArea + LotFrontage, data = train)
summary(pv_model)
Here is code for my regression. SalePrice, MSSubClass, GarageArea, and LotFrontage are all numeric fields, while LotConfig is a factored variable.
Here is the output of my pv_model:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 98154.64 17235.51 5.695 1.75e-08 ***
MSSubClass 50.05 58.38 0.857 0.391539
LotConfigCulDSac 69949.50 12740.62 5.490 5.42e-08 ***
LotConfigFR2 19998.34 14592.31 1.370 0.170932
LotConfigFR3 21390.99 34126.44 0.627 0.530962
LotConfigInside 21666.04 5597.33 3.871 0.000118 ***
GarageArea 175.67 10.96 16.035 < 2e-16 ***
LotFrontage101 42571.20 42664.89 0.998 0.318682
LotFrontage102 26051.49 35876.54 0.726 0.467968
LotFrontage103 36528.81 35967.56 1.016 0.310131
LotFrontage104 218129.42 58129.56 3.752 0.000188 ***
LotFrontage105 61737.12 27618.21 2.235 0.025673 *
LotFrontage106 40806.22 58159.42 0.702 0.483120
LotFrontage107 36744.69 29494.94 1.246 0.213211
LotFrontage108 71537.30 42565.91 1.681 0.093234 .
LotFrontage109 -29193.02 42528.98 -0.686 0.492647
LotFrontage110 73589.28 27706.92 2.656 0.008068 **
As you can see, the first variables operate correctly. Both the factored and numeric fields respond appropriately. That is, until it gets to LotFrontage. For whatever reason, the model runs the regression on every single level of LotFrontage.
For reference, LotFrontage describes the square footage of the subject's front yard. I have properly cleaned the data and replaced NA values. I really am at a loss for why this particular column is acting so unusually.
Any help is greatly appreciated.
If I download the data from the kaggle link or use a github link and do:
train = read.csv("train.csv")
class(x$LotFrontage)
[1] "integer"
pv_model <- lm(SalePrice ~ MSSubClass + LotConfig + GarageArea + LotFrontage,
data = train)
summary(pv_model)
Call:
lm(formula = SalePrice ~ MSSubClass + LotConfig + GarageArea +
LotFrontage, data = train)
Residuals:
Min 1Q Median 3Q Max
-380310 -33812 -4418 24345 487970
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 11915.866 9455.677 1.260 0.20785
MSSubClass 105.699 45.345 2.331 0.01992 *
LotConfigCulDSac 81789.113 10547.120 7.755 1.89e-14 ***
LotConfigFR2 17736.355 11787.227 1.505 0.13266
LotConfigFR3 17649.409 31418.281 0.562 0.57439
LotConfigInside 13073.201 5002.092 2.614 0.00907 **
GarageArea 208.708 8.725 23.920 < 2e-16 ***
LotFrontage 722.380 88.294 8.182 7.12e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Suggest that you read in the csv again like above.

Do I drop this variable from my GLM? The variable is not significant but its interaction with another is

I'm creating a GLM with quasipoisson distribution and when I do an analysis of deviance one of my variables is not significant, but its interaction with another is. It's my understanding that you include interactions when you expect a relationship between the two, so as one goes up the other will also go up.
Worked.out.vol.hours is Total Time.
AAB...BW is the organisers.
Sorry about the terrible variable names.
Call:
glm(formula = total.debris ~ Beach.Region + Volunteers..n. *
worked.out.vol.hour + Survey.Window + AAB...BW, family = quasipoisson,
data = ltype.all)
Deviance Residuals:
Min 1Q Median 3Q Max
-128.45 -22.71 -10.72 7.98 242.77
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 6.298e+00 4.650e-01 13.544 < 2e-16 ***
Beach.RegionNorth East 5.523e-01 1.142e-01 4.838 1.36e-06 ***
Beach.RegionNorth West 7.873e-01 1.233e-01 6.385 1.92e-10 ***
Beach.RegionNorthern Ireland 6.919e-01 1.554e-01 4.452 8.77e-06 ***
Beach.RegionScotland 6.168e-01 1.023e-01 6.030 1.80e-09 ***
Beach.RegionSouth East 7.663e-01 9.997e-02 7.665 2.27e-14 ***
Beach.RegionSouth West 8.261e-01 1.008e-01 8.196 3.38e-16 ***
Beach.RegionWales 6.714e-01 1.104e-01 6.079 1.33e-09 ***
Volunteers..n. 1.710e-02 1.235e-03 13.852 < 2e-16 ***
worked.out.vol.hour 3.579e-03 6.620e-04 5.407 6.83e-08 ***
Survey.Window2000 3.944e-01 1.893e-01 2.083 0.0373 *
Survey.Window2001 1.199e-01 1.851e-01 0.647 0.5174
Survey.Window2002 1.804e-01 1.773e-01 1.017 0.3090
Survey.Window2003 2.789e-01 1.747e-01 1.596 0.1106
Survey.Window2004 1.441e-01 1.738e-01 0.829 0.4069
Survey.Window2005 1.008e-01 1.722e-01 0.586 0.5581
Survey.Window2006 8.810e-02 1.718e-01 0.513 0.6081
Survey.Window2007 7.097e-02 1.726e-01 0.411 0.6809
AAB...BWAAB Combined -7.903e-01 6.679e-01 -1.183 0.2368
AAB...BWAdopt a Beach -6.070e-01 4.234e-01 -1.434 0.1517
AAB...BWBeachwatch Only -4.539e-01 4.227e-01 -1.074 0.2829
AAB...BWBW Combined -6.548e-01 4.863e-01 -1.347 0.1782
Volunteers..n.:worked.out.vol.hour -2.232e-05 1.586e-06 -14.071 < 2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
(Dispersion parameter for quasipoisson family taken to be 1238.943)
Null deviance: 3637808 on 3737 degrees of freedom
Residual deviance: 2952919 on 3715 degrees of freedom
AIC: NA
Number of Fisher Scoring iterations: 5
When I run the code to see which variables are significantanova(actmod1, test="Chisq")
Analysis of Deviance Table
Model: quasipoisson, link: log
Response: total.debris
Terms added sequentially (first to last)
Df Deviance Resid. Df Resid. Dev Pr(>Chi)
NULL 3737 3637808
Beach.Region 7 141546 3730 3496262 < 2.2e-16 ***
Volunteers..n. 1 255212 3729 3241050 < 2.2e-16 ***
worked.out.vol.hour 1 1227 3728 3239823 0.3196126
Survey.Window 8 17788 3720 3222035 0.0729141 .
AAB...BW 4 27536 3716 3194499 0.0001807 ***
Volunteers..n.:worked.out.vol.hour 1 241579 3715 2952919 < 2.2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
worked.out.vol.hours is not significant in the analysis of deviance, but its interaction with Volunteers..n. is, which is expected since the total hours surveyed will naturally increase with more volunteers. I, however want to keep these values separate in the model. How do I go about this issue? Do i just drop the variable altogether? Or do I keep it in because the interaction is significant?
Also, any help with how to succintly report these values would be greatly appreciated since I am quite new to this.

Resources