R: visreg for gam with interactive variables - r

Hi I am currently estimating happiness and age using the gam model.
My command for the regression is
fit <- gam(happy ~ s(age) + s(age, by=nochild) ,data=happy)
and I would like to visualise the estimated effect of the interactive variable "s(age):nochild" using visreg. I am struggling to do so as only the estimation for age and nochild has shown up when using
visreg(fit)
I want the graph to look like this
Y axis : s(age):nochild
X axis : age
However, I am not sure how to do this with visreg or is there any other commands that can help to do this?
Thank you so much in advance

I got the code that I need now so I think I might share this.
plot(fit, select=2, main="No Children")

Related

How to convert the y-axis of a plot from log(y) to y

I'm an R newbie. I want to estimate a regression of log(CONSUMPTION) on INCOME and then make a plot of CONSUMPTION and INCOME.
I can run the following regression and plot the results.
results <- lm(I(log(CONSUMPTION)) ~ INCOME, data=dataset)
effect_plot(results, pred=INCOME)
If I do this, I get log(CONSUMPTION) on the vertical axis rather than CONSUMPTION.
How can I get a plot with CONSUMPTION on the vertical axis?
Another way to ask the question is how do I convert the y-axis of a plot from log(y) to y? While my question is for the function effect_plot(), I would be happy with any plot function.
Thanks for any help you can give me.
Thank you for the responses. I was able to figure out a workaround using Poisson regression:
results1 <- glm(CONSUMPTION ~ INCOME+WEALTH, family=poisson, data=Consumption )
effect_plot(results1,pred=INCOME,data=Consumption)
This allows me to identify the effect of one variable (INCOME) even when the regression has more than one explanatory variable (INCOME+WEALTH), and plots the estimated effect with CONSUMPTION on the vertical axis rather than ln(CONSUMPTION), with INCOME on the horizontal axis.
The associated estimates are virtually identical to what I would get from the log-linear regression:
results2 <- lm(I(log(CONSUMPTION)) ~ INCOME+WEALTH, data=Consumption )
I appreciate you for taking the time to help me with my problem.

Plotting Marginal effects of interactions with continuous and dummy

I have a doubt about my homework on how to plot multiple interactions and I would be very grateful if anyone could help. I know this is a popular topic, but I have not seen questions addressing my specific problem. I hope is not a repetition
I have a model with 3 explanatory variables and two interactions. Two variables are continuous and one is a dummy. I would like to plot into a single image the marginal effect of the three interactions.
the model is something like this
y = a+ b_1c1 + b_2c2 + b_3d + b_4c1c2 + b_5c1d
In sum, I have one continuous variable (c1), that is interacted both with the other continuous variable (c2) and the dummy variable (d).
The code below uses data from the MASS package to allow a reproducible example.
library(interplot)
library(MASS) # to take the dataset from
data<- cars93
library(interplot) # for plotting interations
and I create my model
model<- lm(Price ~ Horsepower*EngineSize # c1*c2 interaction
+ Horsepower*Man.trans.avail, #c1*d interaction
data = data)
I would like to plot the estimated effects of the Horsepower on price as a function of the EngineSize for Man.trans.avail=0 (No),
and plot that same conditional effect of Horsepower on price as a function of the EngineSize for Man.trans.avail=1 (Yes). Possibly putting them into the same plot.
Using the interplot function I am able to see the conditional Horsepower on price as a function of the EngineSize but without controlling for Man.trans.avail
interplot(m=model, var1 = "Horsepower", var2 = "EngineSize", hist = TRUE)
The result plots the marginal effect line of Horsepower and EngineSize, however, is not able to plot two lines for different values of Man.trans.avail and I was wondering whether you had some idea on how to plot marginal effects in similar circumstances.
I thank you a lot in advance for your replies

The residuals vs fitted values plot of ARIMA model in r

I fitted an ARIMA model like follows:
arima_pri <- Arima(prits, order=c(7,1,0), xreg = t2, seasonal=list(order=c(1,1,1), period=12))
And want to look at the residuals vs fitted values plot:
plot(fitted(arima_pri), arima_pri$residuals)
then got this
Also tried ts.plot:
ts.plot(fitted(arima_pri), arima_pri$residuals)
then got this
It's still weird! I want to get a plot like
See http://rstudio-pubs-static.s3.amazonaws.com/21465_653278de4ce44fefa846002156e9b10a.html
Or,
(See http://rstudio-pubs-static.s3.amazonaws.com/21465_653278de4ce44fefa846002156e9b10a.html)
I'm sure all the libraries have been loaded correctly like "forecast". What should I do now? Thank you!

Plotting nonlinear regression in R with logistic growth

I have been working with the CDC FluView dataset, retrieved by this code:
library(cdcfluview)
library(ggplot2)
NEflu <- get_flu_data("census", "1", "ilinet", years=2009)
What I am trying to do is plot the number of providers versus the total number of ILI patients (NUM..OF.PROVIDERS vs ILITOTAL). I was able to generate the model using this code:
z <- glm(NUM..OF.PROVIDERS ~ ILITOTAL, family = poisson(link="log"), data = NEflu)
summary(z)
My first question is, is poisson the correct family for this data possion?
My ultimate goal here is to generate a plot of this data with a line of best fit. It appears to be a logistic growth model from this code:
qplot(ILITOTAL, NUM..OF.PROVIDERS, data=NEflu)
Here is my attempt at generating the fitted line, with the error I received:
ggplot(NEflu, aes(x=ILITOTAL,y=NUM..OF.PROVIDERS)) + geom_smooth()
Error in .Call.graphics(C_palette2, .Call(C_palette2, NULL)) :
invalid graphics state
Thank you for your help.
UPDATE: I do not think using ggplot is going to get me the fitted model I am looking for. Is there a way to use nls() to accomplish this?

Creating interaction effect plot, ggplot or other

I have found an interaction effect between the predictors age and education level in a multiple regression model assessing the effects of various predictors on alcohol consumption. I wish to graph this interaction effect using ggplot, but an alternative will do.
I have attempted to do it this way:
p <- ggplot(DataFrame, aes(ED,AGE, label=interaction-effect))
p <- p + geom_point(colour= "red")+geom_text(size=3) # colour = colr does not work
p
This is continuously spouting errors. I cannot seem to find a way to simply make this plot and would greatly appreciate help.
A straight-forward google search yields fairly good results:
http://www.r-bloggers.com/visual-interpretation-of-interaction-terms-in-linear-models-with-ggplot-rstats/
Interaction Plot in ggplot2
https://stats.stackexchange.com/questions/9477/how-to-draw-an-interaction-plot-with-confidence-intervals
http://www.cookbook-r.com/Graphs/Plotting_means_and_error_bars_(ggplot2)/#error-bars-for-within-subjects-variables

Resources