Defining a constant as a coefficient from an r regression [duplicate] - r

This question already has answers here:
Extract regression coefficient values
(4 answers)
Is there a reason to prefer extractor functions to accessing attributes with $?
(2 answers)
Closed 3 years ago.
I am trying define a constant as a coefficient from an r regression. I.e. trying to define elasticity as the number -1.64431 that you can see in the photo.
reg <- lm(ln_q ~ ln_price, data=df)
elasticity <- reg[["coefficients","ln_price"]]
print(elasticity)
I get the error:
Error in reg[["coefficients", "ln_price"]] :
incorrect number of subscripts
Any help much appreciated! :)

elasticity <- reg$coefficients[names(reg$coefficients)=="ln_price"]

Related

How to get the "Proportion Var" line from PCA output using principal from package psych? [duplicate]

This question already has answers here:
psych: principal - loadings components
(1 answer)
Extracting output from principal function in psych package as a data frame
(2 answers)
Closed 5 years ago.
I would like to get the the "Proportion Var" line like object or vector from "pca $ loadings" below, to use its values in the PCA graphic.
I did the following:
library(psych)
data(iris)
pca <- principal(iris[1:4],nfactors = 2,rotate="varimax",scores=TRUE)
pca$loadings
How should I get the proportional var?
Another way is to compute manually, but first you need to extract all factors (e.g., all axes)
You should specify the nfactors as the number of variables that you have, which in your example is 4. So it should be like like this:
pca <- principal(iris[1:4],nfactors = 4,rotate="varimax",scores=TRUE)
Then, extract the pca$loadings after which you can compute the proportional variance by getting the sum of squared loadings per component (RC in this case) and divide them by the total sum squared loadings, which you can do by this:
colSums(pca$loadings[ , ]^2)/sum(pca$loadings[ , ]^2)
This should give you the same information as the proportion variance in the pca$loadings, albeit for all the components (RC1 to RC4 in this case).

Continuous independent variable using Lm function in R [duplicate]

This question already has an answer here:
Datatype for linear model in R
(1 answer)
Closed 5 years ago.
I ran a regression in data set cwm. Intention:
Test the correlation between cm14 (continuous) and medran (continuous)
Control for type, disint, leverage, and end, which affect cm14
ranking <- lm(cm14 ~ medran + cm10a + type + disint + leverage + end, data=cwm)
summary(ranking)
The summary results gave an independent regression figure for every category in medran.
How can I obtain a figure for the overall impact of medran on cm14?
If any of your variables is categorical, you can use anova(ranking) to do this.

Limit the range of forecast values for R forecast package [duplicate]

This question already has an answer here:
How to specify minimum or maximum possible values in a forecast?
(1 answer)
Closed 6 years ago.
I am performing time series modeling and plotting the eventual forecasts using R's forecast package and the base plot() function.
The final forecasts are negative although this doesn't make sense for the data (the floor is 0). Is there a way for me to tell the forecast function to limit the y-value prediction?
After writing it out, I helped myself find the right search terms. Here is the answer:
http://robjhyndman.com/hyndsight/forecasting-within-limits/

ezANOVA: access to aov content [duplicate]

This question already has answers here:
ezANOVA and pairwise.t.test in R: output
(2 answers)
Closed 7 years ago.
I am using ezANOVA to calculate ANOVAs. I would like calculate a Bayesian measure, which makes use of the values returned in the aov object.
However, I have difficulties accessing the values that are returned in the aov object and consequently do not know how to address them in the function I use for the Bayesian measure.
Let me give an example...
data("ANT") ## example data
rt_anova = ezANOVA(
data = ANT[ANT$error==0,]
, dv = rt
, wid = subnum
, within = .(cue,flank)
, return_aov = T
)
rt_anova
We now get the following for the main effect of cue:
Stratum 2: subnum:cue
Terms:
cue Residuals
Sum of Squares 225485.61 8970.99
Deg. of Freedom 3 57
I now need to access the Degrees of Freedom as well as the Sums of Squares, but I currently have no clue how I must admit (they do not seem to accessible via something like
rt_anova$aov$........
Any suggestions are very welcome!
THANKS!
If you look at rt_anova$aov, you'll see it is of class aovlist. (class(rt_anova$aov)). Do some further exploration (names(rt_anova$aov)), and you can figure out what you need might be rt_anova$aov$'subnum:cue'

Can I calculate z-score with R? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
R, correlation: is there a func that converts a vector of nums to a vector of standard units
By reading stackoverflow's comments, I found z-score maybe calculated with Python or perl, but I did not comes across any for R yet. Did I miss it? Is it possible to be done with R?
As (http://en.wikipedia.org/wiki/Standard_score.)
z-score = (x-μ)/σ
x is a raw score to be standardized;
μ is the mean of the population;
σ is the standard deviation of the population.
I believe there are R packages designed for this? Where can we found them? Or similar package for normalization?
if x is a vector with raw scores then scale(x) is a vector with standardized scores.
Or manually: (x-mean(x))/sd(x)

Resources