Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
476 views
in Technique[技术] by (71.8m points)

r - How to do a regression of a series of variables without typing each variable name

I want to run a regression with a bunch of independent variables from my dataset. There are a lot of predictors, so I do not want to write them all out. Is there a notation to span multiple columns so I don't have to type each?

My attempt was doing this (where my predictors are column 20 to 43):

modelAllHexSubscales = lm(HHdata$garisktot~HHdata[,20:43])

Obviously, this does not work because HHdata[,20:43] is a matrix of data, whereas I really need it to see the data as HHdata[,20]+HHdata[,21] etc.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Here's another alternative:

# if garisktot is in columns 20:43
modelAllHexSubscales <- lm(garisktot ~ ., data=HHdata[,20:43])
# if it isn't
modelData <- data.frame(HHdata["garisktot"],HHdata[,20:43])
modelAllHexSubscales <- lm(garisktot ~ ., data=modelData)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...