IDEMSInternational / R-Instat

A statistics software package powered by R
http://r-instat.org/
GNU General Public License v3.0
38 stars 103 forks source link

Adding prediction as a command in the script window #5122

Open rdstern opened 5 years ago

rdstern commented 5 years ago

I give an example of the help I need.

Open the standard survey data set using open from library.

Use the Model > General > Fit Model, or Model > Model dialogues

Fit the lm model with Yield as the response variable and Fert + Variety as the model (say), if you are using General > Fit Model. It is lm( yield ~ Fert + Variety) in the Model > Model dialogue. In either case save the model as Fert_Var. (Or it could simply be last_model.)

Now I load the Prediction package and would like to find the predicted yields for fertiliser levels (0, 2, 4). (I am not sure what it will give me with the model containing the 3 varieties? If necessary I specify the Fert levels as those with Variety = Trad.

How do I write the commands to do this in the script window? Once I know how to do that I am keen to design a dialogue, but first I would like to be able to give the command. Then I can try different options.

There is a second package, called margins that I think we will use as well. It os by the same person as the predictions package. Interestingly it is designed to copy the statistics package stata. I say interestingly, because the new possible menu on survival (by Lily for her MSc project) was also to compare R with stata.

dannyparsons commented 5 years ago

This is an example you can use, with specified values of carat and cut.

diamonds <- data_book$get_data_frame(data_name="diamonds")
m <- lm(price ~ carat + cut, data = diamonds)
dat <- data.frame(carat = c(0, 0.1, 0.2), cut = rep("Good", 3))
pred <- prediction::prediction(m, dat)
head(pred)
View(pred)

Here's another example using the original data, which is equivalent to our saving of fitted values:

diamonds <- data_book$get_data_frame(data_name="diamonds")
m <- lm(price ~ carat + cut, data = diamonds)
pred <- prediction::prediction(m, diamonds)
head(pred)
View(pred)

The data frame that you pass to prediction as the second function must contain all the explanatory columns of the model, but can also contain others.